Layouts and includes

Copy Markdown View Source

A template can extend a layout, filling the layout's blocks with its own, and include other templates. Every language Braces renders by way of Braces.IR does, in its own syntax:

LanguageExtends a layoutA blockThe layout's blockIncludes
Jinja, Nunjucks, Tera, HubL{% extends "base.html" %}{% block title %}…{% endblock %}{{ super() }}{% include "card.html" %}
Django{% extends "base.html" %}{% block title %}…{% endblock %}{{ block.super }}{% include "card.html" with name=value only %}
Twig{% extends "base.html" %}{% block title %}…{% endblock %}{{ parent() }}{% include "card.html" with {name: value} only %}
Liquid{% layout "base" %}{% block title %}…{% endblock %}{{ block.super }}{% include "card" %}, {% render "card", name: value %}
Handlebars{{#> base}}{{#*inline "title"}}…{{/inline}}{{/base}}{{#> title}}…{{/title}}{{> card name=value}}

Rendering

Give the templates a template extends or includes as templates:, by name, to compile/2 or render/3. They're compiled into the template, so it renders on its own:

layout = "<title>{% block title %}Hi{% endblock %}</title><main>{% block body %}{% endblock %}</main>"
page = ~s({% extends "base.html" %}{% block body %}Hi {{ name }}{% endblock %})

Braces.Jinja.render_to_string(page, %{"name" => "Ada"}, templates: %{"base.html" => layout})
#=> {:ok, "<title>Hi</title><main>Hi Ada</main>"}

templates: takes a map of names to source in the template's language, or to compiled Braces.Templates of any language whose values are true and false as its are, or a function of a name that gives {:ok, template} or :error. A template a template extends or includes that isn't there is a Braces.ConversionError at the tag that names it, as is a template named by a variable, which Braces can't know when it compiles.

Layouts extend layouts, as many as you like, and super() in a block is the block it replaces, all the way up. A block the layout doesn't have is left out.

What a template has outside its blocks follows its language: Jinja drops it but sets the variables it sets, so the layout sees them; Liquid, as LiquidJS does, writes it in the layout's block with no name, {% block %}{% endblock %}, Handlebars' {{> @partial-block}}.

An included template sees the template's variables and data where it's included, as Jinja's and Liquid's include do, and the variables it sets are its own in Jinja and set for the template after it in Liquid. Liquid's render, Jinja's without context, and Django's and Twig's only see only what they're given.

In Phoenix, a .jinja or .liquid template extends and includes templates by their path from its directory, with or without its extensions: {% extends "layout" %} in page.html.jinja is layout.html.jinja beside it. A sigil, such as ~JINJA, has no templates to extend or include: one that does is a CompileError.

Converting

Braces.convert/2 writes layouts, blocks and includes in the other language's syntax, as the table above has them, so a Jinja layout converts to a Liquid one. What a language can't say is a Braces.ConversionError: Handlebars has no super(), and Nunjucks and Tera can't give an include variables of its own.

Handlebars' layouts are partials: a layout's blocks are partial blocks, {{#> title}}Hi{{/title}}, which render the inline partial of their name that the template calling the layout defines, {{#*inline "title"}}…{{/inline}}, or their own content. Within one layout, that's what a Jinja layout does; a Handlebars template's inline partials take the place of those of the templates it's given to, so a layout that extends another, and fills a block that the template extending it fills too, renders the layout's.