Extending Handlebars.js templates

2019-05-07 01:11发布

问题:

Is there a way to extend templates like in Django? My base template has a header that only needs to be a few pages. I'd like to change that for the the other templates.

Something similar to

{% extends "base.html" %}
    ...
{% endblock %}

I'm using Ember.js.

回答1:

As far as i know this notation does not exist, also i haven't seen the concept of inheritance on handlebars templates layer.

However, i can think of two ways to achieve what you want,

1. using the {{partial}} helper http://emberjs.com/guides/templates/rendering-with-helpers/

The {{partial}} helper can render the header part and it can be included only on those templates of the pages that require the header.

2. using layouts http://emberjs.com/guides/views/adding-layouts-to-views/

Have two layouts one with the header and another without it, then specify on the pages/views that need the header the corresponding layout using the layoutName property.



回答2:

I was looking for the same as I come from the same Django background. Here I've found exactly what you are looking for. It uses another module from npm called Handlebar-layouts. which is really useful.