iLemming
iLemming

Reputation: 36166

coffeescript multiline strings compile into multiline strings

How come that this string

"answer 
 to life 
 the universe 
 and everything
 is
 #{40+2}
"

compiles into

"  answer   to life   the universe   and everything  is  " + (40 + 2) + "";

how can I force coffescript to keep it multiline (keeping string interpolation intact):

 "answer \ 
 to life \
 the universe \
 and everything \
 is \
 "+(40+2)

Upvotes: 50

Views: 22319

Answers (2)

HandyAndyShortStack
HandyAndyShortStack

Reputation: 559

I agree it is nice to be able to keep your indentation when defining long strings. You can use string addition for this effect in coffeescript just like you can in javascript:

myVeryLongString = 'I can only fit fifty-nine characters into this string ' +
                   'without exceeding eighty characters on the line, so I use ' +
                   'string addition to make it a little nicer looking.'

evaluates to

'I can only fit fifty-nine characters into this string without exceeding eighty characters, so I use string addition to make it a little nicer looking.'

Upvotes: 22

nzifnab
nzifnab

Reputation: 16092

Try using the heredoc syntax:

myString = """
answer
to life
the universe
and everything
is
#{40+2}
"""

This converts to this javascript:

var myString;

myString = "answer\nto life\nthe universe\nand everything\nis\n" + (40 + 2);

There's not really any point to make it actually be on newlines in the compiled javascript visually, is there?

Upvotes: 78

Related Questions