dthree
dthree

Reputation: 20790

JS - String with Multiple Lines

Say I want to achieve this:

var template = 
"<div>" + 
  "<div class='foo'>" + 
    "How are you?" + 
  "</div>" + 
"</div>";

However, I don't want to keep adding all the quotes and plusses. This gets very boilerplate very fast. I tried this:

var template = 
"<div>
  <div class='foo'>
    How are you? 
  </div>
</div>";

And it didn't fly. I would hope the JS interpreter would ignore the whitespace, but it doesn't look like its okay with it.

Is there any similar way to display a formatted string of HTML across multiple lines without having so much extra junk to type? I can't find one.

Update

There is one suggestion so far, which is clever. I don't know if this is any less efficient or not, and am curious if anyone else has any other ideas.

Upvotes: 0

Views: 89

Answers (1)

j08691
j08691

Reputation: 208040

You can escape the newlines:

var template = 
"<div> \
  <div class='foo'> \
    How are you? \
  </div> \
</div>";

Upvotes: 5

Related Questions