Skip to content

Template Inheritance

harrydeluxe edited this page May 13, 2012 · 1 revision

A new part of PHP-Liquid is template inheritance. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override, just like you do in Django. It works identically so for more info read the Django docs on template inheritance.

Base Template

{% comment %} This is the base template. {% endcomment %}
<!DOCTYPE HTML>
<html>
  <head>
    {% include 'header' %}
  </head>
  <body>
    <div id="content">{% block content %}{% endblock %}</div>
    <div id="footer">
      {% block footer %}
        &copy; Copyright 2012 by <a href="http://www.delacap.com/">DELACAP</a>.
      {% endblock %}
    </div>
  </body>
</html>

Child Template

A child template might look like this:

{% comment %} This is the child template. {% endcomment %}
{% extends "base" %}

{% block content %}
  <h2>Entry one</h2>
  <p>This is my first entry.</p>
{% endblock %}

{% block footer %}
  {{ document.copyright }}
{% endblock %}

The extends tag is the key here. It tells the template engine that this template "extends" another template. When the template system evaluates this template, first it locates the parent. The extends tag should be the first tag in the template.

Clone this wiki locally