0

How exactly do I use an external template for my Marionette ItemView? I read so many 'methods' I can't get any of them working...

JST Method ... I'm not on rails so can't use this

Backbone.Marionette.Renderer.render = (template, data) ->
    path = JST["backbone/apps/" + template]
    unless path
        throw "Template #{template} not found!"
    path(data)

RequireJS Method ... I'm not using require js

var tpl = require(inject[this.templateName]);
this.template = _.template(tpl);

What method should I be using?

user391986
  • 29,536
  • 39
  • 126
  • 205

1 Answers1

1

I use Handlebars for my templating engine and hbs to retrieve the precompiled resource via require.js, that way all I have to do is define the template and set it as the ItemView's template; marionette does the rest.

Here's an example

View - welcome.js

define([
    'app',
    'jquery', 
    'backbone',
    'marionette', 
    'hbs!templates/welcome'
    ],
function(App, $, Backbone, Marionette, template) {

    return Backbone.Marionette.ItemView.extend({

        template: template,
    });
});

Template - welcome.html

<div>
    <h1>Hello World</h1>
</div>

If you're not using handlebars, or can't use hbs, then something similar can be done with text.js - https://github.com/requirejs/text

Jon Miles
  • 9,605
  • 11
  • 46
  • 66