"Current" variable for fields in both Pages and Sections

I’m not sure if this exists already, but I wasn’t able to find it in the docs. Would it be possible to have a common variable current that checks whether it’s a page or a section? This would be similar to current_path and current_url, but covering more variables and fields.

A number of shared terms exist in both sections and pages, and this could simplify writing templates.

As an example, in the <head> of my base.html, I have this block:

<title>{% block head_title %}{% endblock head_title %}</title>

In my templates for pages, I define this:

{% block head_title %}{{ page.title | safe }} | {{ config.title | safe }} | {{ page.description | safe }}{% endblock head_title %}

For sections, this block would be:

{% block head_title %}{{ section.title | safe }} | {{ config.title | safe }} {{ section.description | safe }}{% endblock head_title %}

Both of these could be replaced by a single line in base.html

<title>{{ current.title | safe }} | {{ config.title | safe }} | {{ current.description | safe }}</title>

Related issue: Templates need a variable that could be “section” or “page” · Issue #687 · getzola/zola · GitHub

1 Like

I knew I hadn’t searched hard enough :slight_smile:

Thanks! This works exactly as expected.

{%- if page %}
	{%- set resource = page %}
{%- elif section %}
	{%- set resource = section %}
{%- endif %}