Pages

Thursday, October 8, 2015

About multi-line strings in javascript

It happened that I was working on a project where I kept html templates as strings in typescript files. Some like this:

var template = 
"<div class='game-panel-content'>" +
"<ul class='fleet-list' data-bind='foreach: availableShips'>" +
"<li data-bind='click: $parent.selectShip'>" +
"<span data-bind='text: name' />" +
"</li>" +
"</ul>" +
"</div>";

I want to keep the indentation for readability, the bad news is that CodeMaid (one of the VisualStudio extensions that I use to clean up the code) doesn't understand this multi-line string and removed the indentation :(


The good news is that there is another way to write a multi-line string in javascript that is both simpler to write and works well with CodeMaid:


var template =
"<div class='game-panel-content'>\
<ul class='fleet-list' data-bind='foreach: availableShips'>\
<li data-bind='click: $parent.selectShip'>\
<span data-bind='text: name' />\
</li>\
</ul>\
</div>";

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.