0

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.

1 Answer 1

5

You can escape the newlines:

var template = 
"<div> \
  <div class='foo'> \
    How are you? \
  </div> \
</div>";
Sign up to request clarification or add additional context in comments.

1 Comment

Clever! Never thought of that. Do you know if this would make the parsing any less efficient?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.