javascript - Handlebars variables for cleaner templates (handlebars.js)? -


so code can make html ugly , confusing.

{{#if user.facebook.id}}      <img class="img-rounded" src="https://graph.facebook.com/{{user.facebook.id}}/picture"/> {{else}}      <img class="img-rounded" src="http://www.gravatar.com/avatar/{{user.local.gravatar}}?s=50"/>  {{/if}} 

in javascript could

var picture;  if(user.facebook.id) {      picture = '<img class="img-rounded" src="https://graph.facebook.com/'+user.facebook.id+'/picture"/>'; } else {      picture = '<img class="img-rounded" src="http://www.gravatar.com/avatar/'+user.local.gravitar+'?s=50"/>'; }  return picture; 

you know , call picture , know do. there way in handlebars. used in underscore.js.

do need create handlebars helper method, or there built in can leverage?

handlebar helper method going best bet if dont html version.

{{checkfacebookid user.facebook.id user.local.gravatar}}   handlebars.registerhelper('checkfacebookid', function (id, gravatar) {     var picture = '';     if (id) {          picture = '<img class="img-rounded" src="https://graph.facebook.com/' + id + '/picture"/>';     } else {          picture = '<img class="img-rounded" src="http://www.gravatar.com/avatar/' + gravatar + '?s=50"/>';     }     return new handlebars.safestring(picture); }); 

Comments

Popular posts from this blog

hibernate - How to load global settings frequently used in application in Java -

python 3.x - Mapping specific letters onto a list of words -

objective c - Ownership modifiers with manual reference counting -