mustache/spec — Issue #37: Proposal: Template inheritance
Author: spullara
Created: 2012-02-17 18:10:48+00:00
State: open

At Twitter we run into a very common case where we want to have one template where we maintain the "bones" of the page and each real page on the site merely replaces sections of that page with appropriate content. We would like to avoid putting any business logic in the bones of the page for including the right content, nor do we want to write custom backing code for each of those pages.

My proposal is a declarative syntax for defining replaceable blocks, very much akin to the way this is done in Django and some other templating systems. Here is simple example:

super.mustache:

{{$title}}Default title{{/title}}

{{>navigation}}

{{$content}}Default content of the page{{/content}}

{{>footer}}

sub.mustache:
{{

Profile of {{username}} | Twitter

{{>navigation}}

Here is {{username}}'s profile page

{{>footer}}

There are many ways that people have tried to workaround this limitation including:
- rendering sub templates into {{{content}}} values
- using javascript to render sections separate and splatting them into the DOM
- reproducing the bones in each sub page through a bunch of partials
- creating massive switch statements using sections

All of these allow business logic and code to creep into the template. My proposal to maintain the declarative nature of mustache by adding this very powerful inheritance feature.