3

Trying to use the Heredoc Method described here: http://www.developfortheweb.com/2009/03/multi-line-strings-in-javascript/

var string = (<r><![CDATA[

   The text string goes here.  Since this is a XML CDATA section,
   stuff like <> work fine too, even if definitely invalid XML. 

]]></r>).toString();

I can't make it work on node.js. I tested it on client side - it works on Firefox, but Chrome.

How should I use this method on node.js?

Thanks!

Alexander
  • 7,484
  • 4
  • 51
  • 65
  • This works on Firefox because it supports [E4X](http://en.wikipedia.org/wiki/ECMAScript_for_XML) syntax. But E4X [was deprecated](https://developer.mozilla.org/en-US/docs/E4X), so this is the sole browser to support "heredoc" syntax. – CedX Feb 01 '14 at 10:37
  • Nowadays the solution is obviously Template Literals (it didn't exist when I asked this questions). So to have a multi-line string in JS - just surround it by back-ticks. – Alexander Jun 04 '18 at 09:49

3 Answers3

2

Even though this blog posts tells you something else, JavaScript does not have heredoc strings.

So you should not use it at all - it is a dirty hack. The reason why it works in some browsers is that they allow inline XML. NodeJS probably doesn't because well, it's ugly and dirty.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    For use with Node, there are other dirty hacks of course, see http://stackoverflow.com/a/5571069/893780 :) – robertklep Apr 14 '13 at 08:05
2

Now with ES6 it's easy to do that using Template Strings

Alexander
  • 7,484
  • 4
  • 51
  • 65
1

The reason I was looking for it was a need to pass multi-line html text (keeping nice html indents and without splitting it to js strings) as an argument to a template engine.

Finally for my problem I just created a template engine with a heredoc block - so if you anybody is looking for heredoc just for more convenient way to write his templates - the https://github.com/AlexLibs/hot can be used (the module is registered on npm).

Alexander
  • 7,484
  • 4
  • 51
  • 65