Mustache.js: do partials work?

Created on 11 Mar 2010  路  7Comments  路  Source: janl/mustache.js

i have a simple partial, template in the same dir.
tried both (where header.html.mu exists in the same directory as the main template.html.mu


{{foo}}
{{ {{>header.html}}

nothing, works but doesn't include what is in header.html

Most helpful comment

the key here is that you will have to have loaded all the template files from somewhere before calling to_html() and pass in all the template's contents:

var view = {
partial: {
foo: 1
},
name: "Jan"
};

var template = "Hi {{name}}. you have {{>partial}} foos.";

var partial_html = "{{foo}}";

Mustache.to_html(view, template, {
partial: partial_html
});

... I think you should add this to the documentation - I spent over an hour scratching my head over why partials didn't work - I like simple and clear documentation as much as anyone but not at the expense of leaving out essential information on how to use a basic functionality

All 7 comments

Partials do work, check out the examples in the examples/ directory as well as the docs in the Readme. mustache.js doesn't automatically load any files in the same directory (as JavaScript doesn't have any file-io :)

what does "automatically load any files" mean? so lets' say i have:

view.js
templates/header.html.mu
templates/body.html.mu

in view.js,i have : Mu.templateRoot = './templates';

then i render "header.html".

header.html.mu has

{{>body.html}}

but it never gets included. am i missing something? is there some directory path munging going on?

can you point me to a specific js example that runs a partial? I see the markup .html.mu but not any js that does anything with them...
thanks,
thanks....

oh, i see test.js. so are you saying that we must use readFileSYnc and do a compile of the template, versus doing render that has partials?

continued struggle. is there a simple example that runs under nodejs that uses partials in the templates?

the key here is that you will have to have loaded all the template files from somewhere before calling to_html() and pass in all the template's contents:

var view = {
  partial: {
    foo: 1
  },
  name: "Jan"
};

var template = "Hi {{name}}. you have {{>partial}} foos.";

var partial_html = "{{foo}}";

Mustache.to_html(view, template, {
   partial: partial_html
});

/nevermind.

the problem was, my template had a

, which was blowing up the compiler....it apparently needs to be ' quotes:
in the partial...

the key here is that you will have to have loaded all the template files from somewhere before calling to_html() and pass in all the template's contents:

var view = {
partial: {
foo: 1
},
name: "Jan"
};

var template = "Hi {{name}}. you have {{>partial}} foos.";

var partial_html = "{{foo}}";

Mustache.to_html(view, template, {
partial: partial_html
});

... I think you should add this to the documentation - I spent over an hour scratching my head over why partials didn't work - I like simple and clear documentation as much as anyone but not at the expense of leaving out essential information on how to use a basic functionality

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amper5and picture amper5and  路  5Comments

barbalex picture barbalex  路  5Comments

mbrodala picture mbrodala  路  16Comments

rlightner picture rlightner  路  7Comments

kuldeepdhaka picture kuldeepdhaka  路  9Comments