Search code examples
javascripthtmlmarionette

How to pass a line break in Marionette templateHelper / templateContext?


For the given view, in Marionette 2.4.4:

var view = Marionette.LayoutView.extend({
  template: Handlebars.compile('{{body}}'),
  templateHelpers: function(){
    return {
      body: "Line one. Line Two.",
    };
  }
});

view = new view();
MasterView.showChildView('master_content', view);

What do I need to add to the "body" property to make "Line one." appear on a line above "Line Two." once rendered?

Note: templateHelpers became templateContext in newer versions of Marionette.

Experiments: <br> does not work. It is simply shown as plaintext.


Solution

  • The reason <br> didn't work is due to Handlebars, not Marionette. As per this stack overflow question, to make Handlebars not escape html expressions, use {{{}}} triple braces, instead of {{}}. Thus, the following code works:

    var view = Marionette.LayoutView.extend({
      template: Handlebars.compile('{{body}}'),
      templateHelpers: function(){
        return {
          body: "Line one. {{{<br>}}} Line Two.",
        };
      }
    });
    
    view = new view();
    MasterView.showChildView('master_content', view);