HTML5 Zone is brought to you in partnership with:

Troy Miles is a Southern California based software developer. He began by writing games for the Apple II, C64 and IBM PC in assembly language over 30 years ago. After burning out on games he switch to creating windows application software in C and C++. And now he develops applications for both web, mobile web and mobile devices in C#, Objective C, Java, and JavaScript. He is also a big fan of jQuery, jQuery Mobile and PhoneGap. Troy is a DZone MVB and is not an employee of DZone and has posted 25 posts at DZone. You can read more from them at their website. View Full User Profile

Quick and Dirty Server Side Select Helper for Handlebars.js

11.12.2013
| 2870 views |
  • submit to reddit
Handlebars.js is one of  my favorite templating engines. One of the main reasons is because it is usable both on the client and server side.  While the client-side is pretty familiar, the server-side has its challenges, like having no DOM. Having no DOM means that some convenient methods like createElement don't exist, but there are times when being able to deliver a render-ready HTML page is important. 



In one of my projects, we wanted to do server-side rendering of an edit page. Nothing tricky or flashy, just send the user the data they entered previously for editing. This kind of a page is easy with just about every templating engine around. One problem HTML element is the select tag, because it is a composite element consisting of <select> enclosing one or more <option> tags. Luckily handlebars.js includes the ability create your own helpers, which are bits of code you write yourself and handlerbars invokes. Writing select helpers is pretty easy on the client since you have access to the DOM.

Most client-side helpers will generate the select and each of the options including their value and selected attributes.  I initially was trying to go this route on the server-side as well. And then I stopped myself. The lack of DOM access caused the code to quickly get more complicated than I liked. So I decided to go another way. Instead of rendering out the select element, I instead decide to render the attributes of the option element. This way my helper is much simpler. To use it, simply add:

{{option <value of option> <current value>}}

to each option element. The helper will render out the value attribute and, if it is the currently selected option, it will render the selected attribute as well.

I may change my mind and render the entire select element, but for now I like this simpler approach.
// Render an option tag's value and selected attributes
hbs.registerHelper("option", function (current, field) {
  current = hbs.Utils.escapeExpression(current);
  field = hbs.Utils.escapeExpression(field);
  var results = 'value="' + current + '" ' + (field === current ? 'selected="selected"' : "");
  return new hbs.SafeString(results);
});

<select id="goal2" name="goal2">
  <option value="">Select goal</option>
  <option {{option "cplp2" campaign.goal2}}>Cost per Lead</option>
  <option {{option "cpap2" campaign.goal2}}>Cost per Acquisition</option>
  <option {{option "cpip2" campaign.goal2}}>Cost per Inquiry</option>
</select>


Published at DZone with permission of Troy Miles, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)