Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Extending templates with extends and block

Start with a template that contains one or more blocks:

<!DOCTYPE html>
<main>
{%- block content %}
  <p>Parent content.</p>
{% endblock -%}
</main>

Then create a template to extend it:

{% extends "layout.html.oxip" %}

This is essentially the same as using layout.html.oxip directly:

<!DOCTYPE html>
<main>
  <p>Parent content.</p>
</main>

Named endblock

endblock tags can also include the name of the block that it is supposed to be ending.

<!DOCTYPE html>
<main>
{%- block content %}
  <p>Parent content.</p>
{% endblock content -%}
</main>

This can help in situations where an endblock tag gets added or removed accidentally:

#[derive(Oxiplate)]
#[oxiplate_inline(
    r#"{% extends "extends-wrapper.html.oxip" %}
{% block content -%}
    <p>{{ message }}</p>
    {%- parent -%}
    <p>{{ message }}</p>
{%- endblock aside %}
"#
)]
struct Data {
    title: &'static str,
    message: &'static str,
}
error: Expected to end block `content`, but end tag for block `aside` found instead
  --> tests/broken/tests/extends/endblock-mismatch.rs:9:25
   |
 9 |       <p>{{ message }}</p>
   |  _________________________^
10 | | {%- endblock aside %}
   | |_____________________^

error: "block" statement is never closed (unexpected end of template)
  --> tests/broken/tests/extends/endblock-mismatch.rs:6:1
   |
 6 | / {% block content -%}
 7 | |     <p>{{ message }}</p>
 8 | |     {%- parent -%}
 9 | |     <p>{{ message }}</p>
10 | | {%- endblock aside %}
11 | | "#
   | |_^

Adding to or replacing block content

Anything you add to a block of the same name in a child template will replace the content of the parent block:

  {% extends "layout.html.oxip" %}

+ {% block content %}
+   <p>Replaced content.</p>
+ {% endblock %}
  <!DOCTYPE html>
  <main>
-   <p>Parent content.</p>
+   <p>Replaced content.</p>
  </main>

The parent's content can be kept by using the {% parent %} tag in the block:

  {% extends "layout.html.oxip" %}+

+ {% block(surround) content %}
+   <p>Prefix.</p>
+   {% parent %}
+   <p>Suffix.</p>
+ {% endblock %}
  <!DOCTYPE html>
  <main>
+   <p>Prefix.</p>
    <p>Parent content.</p>
+   <p>Suffix.</p>
  </main>