Go to page content

Writing readable templates

Some simple techniques for making templates shorter and easier to read.

Why

A common style and shorthand techniques make templates from multiple authors more alike.  This makes them easier to maintain and share in a team or with the community.  This guide exists to present some techniques and advice for writing readable templates.

Assumptions

Readers are assumed to be comfortable reading and writing Zotonic templates.

How

When the template sees that you request a property of an integer then it assumes that the integer is a m.rsc id. This makes templates more readable.

Example:

<li>
    <h3>{{ m.rsc[id].title }}</h3>
    {% for image_id in m.rsc[id].o.depiction %}
    <figure>
        {% media image_id width=100 link=id %}
        {% if m.rsc[image_id].summary %}
            <p class="image-caption">{{ m.rsc[image_id].summary }}</p>
        {% endif %}
    </figure>
    {% endfor %}
</li>

Can be more effectively written as follows to improve readability:

<li>
    <h3>{{ id.title }}</h3>
    {% for image_id in id.o.depiction %}
    <figure>
        {% media image_id width=100 link=id %}
        {% if image_id.summary %}
            <p class="image-caption">{{ image_id.summary }}</p>
        {% endif %}
    </figure>
    {% endfor %}
</li>

Troubleshooting

There are no troubleshooting steps available for this guide.  Please provide any you have learned in the comments below or on the Zotonic Users Group.

This page is part of the Zotonic documentation, which is licensed under the Apache License 2.0.