12

In the Django framework, web page templates can inherit from other templates. In your child template, you define blocks of code which override like-named blocks in parent templates. I'm guessing there are other back-end templating systems which also work this way, but Django is the one I'm familiar with.

Do any of the existing javascript template systems support django-style inheritance?

3
  • 2
    You should explain what "django-style inheritance" is and then maybe someone will post an answer. Commented Sep 7, 2010 at 1:17
  • Just created my own question regarding this issue, with more detail. Hope this'll help. Commented Aug 10, 2011 at 17:13
  • Have a look at nunjucks.jlongster.com It's inspired by Django's jinja2 and provides a beautiful way of template inheritance. Commented Jun 28, 2013 at 6:27

7 Answers 7

6

What you're looking for if you want template inheritance and DTL syntax, is Nunjucks :

http://mozilla.github.io/nunjucks/

Sign up to request clarification or add additional context in comments.

Comments

0

Mustache.js has support for partials, which work in a similar* manner. Other than that, I haven't been able to find anything -- maybe I'll make it ...

* Okay, they're not really like Django's templates at all, but they are the closest simulacrum I could find.

Comments

0

jQuery Template have a {{wrap}} template tag which is similar to extend:

http://api.jquery.com/template-tag-wrap/

Comments

0

Although it is not "template inheritance", but Beard.js can help you re-use your template by creating template references, which I think can help you achieve the same goal.

http://jspopisno1.github.com/Beard/#exmp_syntax-reference

Comments

0

Check out Handlebars.js. It allows you to define helper methods that you could use to compartmentalize different blocks, and echo them out, in a way similar to Rails' content_for :my_area and yield :my_area.

Comments

0

Yes, with Dustjs, you can have a template inherit the bulk of its content from a common base template. Here is how it works, with Dustjs: (see the "Blocks and Inline Partials" section)

In a file "base_template":

Start
{+title/}
Middle
{+main/}
End

A template that "inherits" the base_template:

{>base_template/}
{<title}
  Child Title
{/title}
{<main}
  Child Content
{/main}

Results in something like:

Start
Child Title
Middle
Child Content
End

Related question: Only 1 level of inheritance seems to be supported: Is there a way to do more than one level of inheritance value overrides with dust.js?

Comments

0

There's some work in progress on adding template inheritance to Handlebars.

See this fairly recent GitHub issue: https://github.com/wycats/handlebars.js/issues/208
And this blog post: Template Inheritance for Handlebars

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.