Search code examples
backbone.jsmarionettebackbone-views

NoTemplateError Backbone.Marrionette but show template in error msg


Here is my error:

Uncaught NoTemplateError: Could not find template: '<!-- HTML Template --> 
<div id="start_div">

<h2>Choose your path to ... // the rest of the template

It is telling me that there is no template but then it outputs the template that it said it could not find.

Here is my code:

require(["jquery", "marionette", "views/StartView" ],
function($, marionette, StartView) {

  var SCApp = new marionette.Application();

  SCApp.addRegions({
      mainRegion: "#center_court"
  });
  var startView = new StartView();
  SCApp.mainRegion.show(startView);
  SCApp.start();

}

Here is the StartView.js

define(["jquery", "marionette", "text!templates/startDiv.html"],

function($, marionette, template){

    var StartView = marionette.ItemView.extend({
       //template: "#start_div"
        template: template
    });

    // Returns the View class
    return StartView;

});

Can anyone see what I'm doing wrong? Do I need something for templating in the require method?

Any suggestion are greatly appreciated.

Andrew


Solution

  • Usually Marionette search for a template inside the DOM with an ID equal to the one you reference in your view, so you have to change the loadTemplate from Marionette.TemplateCache in this way:

    Backbone.Marionette.TemplateCache.prototype.loadTemplate = function(templateId) {
    
        var template = templateId;
    
        if (!template || template.length === 0){
            var msg = "Could not find template: '" + templateId + "'";
            var err = new Error(msg);
            err.name = "NoTemplateError";
            throw err;
        }
    
        return template;
    };
    

    I actually don't remember where I found this function, I can't find it anymore in Marionette's Wiki, anyway it's working fine for me.