Handlebars Split Strings Helper

Handlebars is great.

What is Handlebars I hear you say?

Doh!! Do you embed your HTML code in JS code. Bad….Handlebars to the rescue.

String Splitter

While using Handlebars I came across a scenario where I had to split a string using a delimiter and then push the contents into the DOM individually.

Show me Code

Handlebars.registerHelper("splitString", function(context, options){
      var ret = "";
      var tempArr = context.trim().split(options.hash["delimiter"]);

      for(var i=0; i < tempArr.length; i++)
        ret = ret + options.fn(tempArr[i]);

      return ret;


<script id="template" type="text/x-handlebars">

<script type="text/javascript">
      var context = {sillyString: "big gray box"};
      var templ = Handlebars.compile($("#template").html());
      var html = templ(context);