FoREacH
FoREacH

Reputation: 143

How to wrap text in block, using Ember js and Handlebars js?

I want to wrap some text in block. Cannot figure out how to do it.

JS:

    Handlebars.registerHelper('mywrap', function (options) {
      return '<p class="TEST" >' + options.fn(this) + '</p>';
    });

Template:

    {{#mywrap}}
        This text must be wrapped
    {{/mywrap}}

Final html should be like this:

<p class="TEST">This text must be wrapped</p>

Ember v1.0.0-pre4, Handlebars 1.0.rc.2, Jquery 1.9.0

Upvotes: 2

Views: 1190

Answers (1)

Bradley Priest
Bradley Priest

Reputation: 7458

There are lots of ways of wrapping text in a block, really depends on your usecase. I'm assuming for some reason, literally writing the HTML is not what you're looking for.

Firstly, you can just return a Handlebars SafeString, however this presents a security issue if you are wrapping user-provided content.

Ember.Handlebars.registerHelper('mywrap', function (options) {
  return new Handlebars.SafeString('<p class="TEST" >' + options.fn(this) + '</p>';)
});

Secondly, you can wrap it in a view

{{#view classNames="TEST" tagName="p"}}
  This text must be wrapped
{{/view}}

Thirdly, you can create a handlebars helper that creates a view.

Ember.Handlebars.registerHelper('mywrap', function (options) {
  var view = Ember.View.extend({tagName:"p",classNames:"TEST"})
  return Ember.Handlebars.helper.view.call(this, view, options)
});

Caveat, I haven't actually checked the code so there may be typos

Upvotes: 3

Related Questions