Language Constructs
Within Stacey templates, you have access to language constructs (Tags) such as: set
blocks, for
loops and boolean if
statements. More Tags can be found in the Twig documention.
- set
-
{% set page = get('url') %} # do stuff
- for
-
{% for collection in page.collections %} # do stuff {% endfor %}
- if
-
{% if variable in page.variable %} # do stuff {% endif %}
- if not
-
{% if not variable in page.variable %} # do stuff {% endif %}
Variable Context
Once you are inside a for
loop, the variable context changes to the current object being referenced by the loop.
If you want to temporarily shift to the context of a specific page, you can add a set
block.
{% block project_1 %} {% set page = get('projects/project-1') %} {{ page.page_name }} {% endblock %}
This will change the context from the current page being viewed to the projects/project-1 page. This means page.children will be filled with the children of the projects/project-1 page, page.page_name will equal 'Project 1', etc.
So, if you wanted to only list children of the /projects folder, you could construct the following partial:
{% block projects %} {% set page = get('projects') %} {% for child in page.children if child %} <p><a href="{{ child.url }}">{{ child.page_name }}</a></p> {% endfor %} {% endblock %}
Nesting for Loops
To give you the ability to pass through multiple levels of objects, for loops can also be nested within themselves (although using recursive partials would be a more elegant solution).
<ol id="navigation"> {% for page in page.root %} <li><a href="{{ page.url }}">{{ page.page_name }}</a> <ol> {% for child in page.children %} <li><a href="{{ child.url }}">{{ child.page_name }}</a> {% endfor %} </ol> </li> {% endfor %} </ol>