morgancodes
morgancodes

Reputation: 25265

javascript template inheritance

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?

Upvotes: 12

Views: 3498

Answers (7)

evaisse
evaisse

Reputation: 101

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

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

Upvotes: 6

KajMagnus
KajMagnus

Reputation: 11666

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

Upvotes: 0

KajMagnus
KajMagnus

Reputation: 11666

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?

Upvotes: 0

frontendbeauty
frontendbeauty

Reputation: 2129

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.

Upvotes: 0

Liangliang Zheng
Liangliang Zheng

Reputation: 1785

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

Upvotes: 0

Udi
Udi

Reputation: 30502

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

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

Upvotes: 0

Sean Vieira
Sean Vieira

Reputation: 159905

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.

Upvotes: 0

Related Questions