Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HandlebarsJS How to split a string

I have to split up a string, which is delivered as JSON.

I have the following JSON output:

"title" : "Rihanna - Pon de replay"

And I need to display it like this

PON DE REPLAY
Rihanna

Right now my Handlebars template look like this:

<div>
  {{#each this}}
    <p>{{title}}</p>
  {{/each}}
</div>

Is there someone who could help me out? I would really appreciate it! Thanks in advance...

like image 835
SHT Avatar asked Aug 12 '14 08:08

SHT


1 Answers

You will need to create an helper for this.

Handlebars.registerHelper('splitTitle', function(title) {
  var t = title.split(" - ");
  return t[1] + " <br/> " + t[0];
});

and the tempalte should be like this,

<div>
  {{#each this}}
    <p>{{splitTitle title}}</p>
  {{/each}}
</div>

Edit: To render HTML output, use triple curly braces {{{splitTitle title}}}

<div>
  {{#each this}}
    <p>{{{splitTitle title}}}</p>
  {{/each}}
</div>
like image 117
msapkal Avatar answered Oct 23 '22 05:10

msapkal