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

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -