Search code examples
templatesbackbone.jsmarionette

Marionette JST template rendering


I'm trying to get Marionette render my JST templates, working in Rails environment. According to the tutorial and Marionette official documentation I have to override Marionette render methods:

Backbone.Marionette.Renderer.render = (template, data) ->
path = JST["path/to/template/" + template]
unless path
    throw "error"
path(data)

And when calling template from the view:

    class myChildView extends Marionette.ItemView
      template: "specific-template-location/template"

    class myCompositeView extends Marionette.CompositeView
      template: "specific-template-location/template"
      childView: myChildView

I'm getting an Uncaught error upon rendering. Strangely enough, but when I've used itemView instead of a childView the templates were rendering properly. The tutorial I'm using already proved to be outdated, but I fail to find any correlation between differences of childView \ itemView and template declaration in the official documentation. Any tips would be greatly appreciated.

Additional information: I also can't pass the template directly from the DOM (Marionette render override removed), i.e:

    class myCompositeView extends Marionette.CompositeView
      template: "#mytemplate"

Also throw a no template error. The only way I've managed to pass a template is through an Underscore template constructor - _.template(), which is at least indicates that there are no issues with passing a collection to the view.


Solution

  • You could make like this:

    do (Marionette) ->
      _.extend Marionette.Renderer,
      lookups: ['path/to/template/apps', 'path/to/template/components']
    
      render: (template, data) ->
        return unless template
        path = @getTemplate(template)
        throw "Template #{ template } not found!" unless path
        path(data)
    
      getTemplate: (template) ->
        for lookup in @lookups
          path = "#{ lookup }/#{ template }"
          return JST[path] if JST[path]
    

    I have one example here, using Rails too