Dust-motes

HELPERS / HTML

layout

{@layout base=string} blocks {/layout}

Page layout support with multi-level inheritance

Parameters:

  • base : name of parent template

dust provides a simple mechanism for defining a base template with default values for replaceable areas of the template. A child template can then specify it's parent (or base) template. However, the way it is handled allows only one level of inheritance and has problems if you want to dynamically select which parent template you want to use.

This helper leverages the existing dust base template mechanism {+name} for defining areas of the template that can be overriden by a child template. If you have a base template, you can continue to use it, as is. The child template uses the base parameter to specify the name of the parent template (partial). Within the body of the @layout helper, supply one or more {:blocks} with names matching the {+name} block in the parent that you want to override. Any blocks you don't override, get the parent's default value.

Examples:


base_template
Start{~n}{+title}Base Title{/title}{~n}{+main}Base Content{/main}{~n}End

child_template
{@layout base="base_template"}{:title}Child title{:main}Child Content{/layout}

extend_child_template
{@layout base="child_template"}{:title}Childish title{:main}Childish Content{/layout}

will output
Start
Childish title
Childish Content
End

If the extend_child_template was
{@layout base="child_template"}{:main}Childish Content{/layout}

the output would be
Start
Child title
Childish Content
End

if extend_child_template and child_template omit the {:title} block the output is
Start
Base Title
Childish Content
End