Can Mustache Templates do template extension?

29,012

Solution 1

I recently found myself in the same boat, except I came from a mako background.

Mustache does not allow for template extension/inheritance but there are a few options available to you that I know of.

  1. You could use partials:

    {{>header}}
        Hello {{name}}
    {{>footer}}
    
  2. You could inject template pre-processing functions into the context for each template that needs to inherit from some other page:

    {{#extendBase}}      
        Hello {{name}}
    {{/extendBase}} 
    

    Hash:

    {
       "name": "Walden",
       "extendBase": function() {
           return function(text) {
               return "<html><head></head>" + render(text) + "</body></html>"
           }
       }
    }
    
  3. Prepend and append the desired HTML to the relevant pages in your controller.

  4. Have a layout template ala:

    {{>header}}
        {{{body}}}
    {{>footer}}
    

    And render the body in your controller, passing that to the layout template as a variable named body.

  5. Implement template inheritance, pre-mustache, in your code that loads templates.

I wouldn't, however, use the triple mustache because I don't want unescaped HTML to be appearing anywhere, it's just too risky in my opinion.

If someone else has a better solution to this problem I'd love to hear it as well, since I haven't yet taken the plunge in any one of these directions.

Solution 2

I've proposed this to the specification for Mustache here:

https://github.com/mustache/spec/issues/38

Currently mustache.java, hogan.js and phly_mustache support template inheritance.

Solution 3

You could use variables containing HTML. A "triple mustache" like {{{variable}}} will return unescaped HTML. It's not exactly the same as template extensions, but you could render frontpage-content.html and then put its output in a content variable that gets passed to base.html.

(I added -content to the frontpage.html filename with the expectation that such a naming pattern will help keep the filenames manageable.)

Solution 4

Mustache doesn't do template extension.

If you really want template extension then you may want to use a library purpose built with this functionality for you language/framework of choice.


FYI, I'm using Node.js/Express, so I will probably end up using https://github.com/fat/stache

Solution 5

In mustache php, template inheritance is supported since version 2.7.0.

https://github.com/bobthecow/mustache.php/wiki/BLOCKS-pragma

You can figure out your current version from the file Mustache/Engine.php and search for the line containing:

class Mustache_Engine
{
    const VERSION        = '2.8.0';
    ...
Share:
29,012
Chris W.
Author by

Chris W.

Python was my first love, but Javascript is growing on me.

Updated on July 08, 2022

Comments

  • Chris W.
    Chris W. almost 2 years

    I'm new to Mustache.

    Many templating languages (e.g., Django / Jinja) will let you extend a "parent" template like so...

    base.html

    <html><head></head>
        <body>
        {% block content %}{% endblock %}
        </body>
    </html>
    

    frontpage.html

    {% extends "base.html" %}
    {% block content %}<h1>Foobar!</h1>{% endblock %}
    

    rendered frontpage.html

    <html><head></head>
        <body>
        <h1>Foobar!</h1>
        </body>
    </html>
    

    I'm aware of Mustache's partials (e.g., {{>content}}), but those seem to be just includes.

    Does template extension exist for Mustache? Or, failing that, is there at least some design pattern that effectively turns includes into template extension equivalents.