2

I am making web application, in which a lot of content generated from json in JS. I don't know how it's done right, so I just generate complex html fragments like a strings. It does't seems the best way, because it loose a lot of good things plain html has: syntax highlighting, tabulation, plus symbol after every line:

 $('#chatlist').append(
 "<div class='chatlist_cell'>" +
 "<div class='chatlist_cell_inner'>" +
 "<div style='float: left;'>" +
 "<img class='chatlist_avatar' src='/images/ui/user_placeholder.svg'>" +
 "</div>" +
 "<div style='float: left; padding-left: 10px;'>" +
 "<div class='chatlist_title'>" + title + "</div>" +
 "<div class='chatlist_text'>" + fromUserString + message.text + "</div>" +
 "</div>" +
 "</div>" +
 "</div>"
 )

Today I try the same with jQuery, but it's even worse. Much more complicated to understand.

 $('#chatlist').append(
 $('<div />').addClass('chatlist_cell').append(
 $('<div />').addClass('chatlist_cell_inner').append(
 $('<div />').css('float', 'left').append(
 $('<img />').addClass('chatlist_avatar').attr('src','/images/ui/user_placeholder.svg')
 )
 ).append(
 $('<div />').css({'float': 'left', 'padding-left': '10px'}).append(
 $('<div />').addClass('chatlist_title').text(title),
 $('<div />').addClass('chatlist_text').text(message.text)
 )
 )
 )
 )

Is there any better way in JS/jQuery? And if not, is there any other tools?

asked Jun 20, 2015 at 18:29
2

1 Answer 1

4

If you are not willing/able to use a full-fledged JS framework (no problem with that) you may want to look into underscore/lodash templates which is pretty lightweight and saves you from writing code to modify the dom into every possible state. You just outline how your data should be rendered and then just re-render it if you data changes. Note this should not be used for "web app" type apps as JS frameworks are much more suited to that job (React/Flow, Angular, Backbone, Ember, etc) but for generating HTML for small or rarely changing interfaces lodash is very nice.

Example:

From your code:

<script id="myTemplate" type="text/template"> 
 <div class='chatlist_cell'>
 <div class='chatlist_cell_inner'>
 <div style='float: left;'>
 <img class='chatlist_avatar' src='/images/ui/user_placeholder.svg'>
 </div>
 <div style='float: left; padding-left: 10px;'>
 <div class='chatlist_title'><%=title%></div>
 <div class='chatlist_text'><%=fromUserString%> <%=message.text%></div>
 </div>
 </div>
 </div>
</script>
<script>
 $(function() {
 var myTemplate = _.template($('#myTemplate').html());
 function addNewMessage(title, message, fromUserString) {
 var html = myTemplate({
 title: title,
 message: message,
 fromUserString: fromUserString
 });
 $('#chatlist').append(html);
 }
 addNewMessage("myTitle", {text: "myText"}, "JoshStrange:");
 });
</script>

JSFiddle

answered Jun 20, 2015 at 18:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.