Control structures
Control structures in Twig allow you to manage the flow of your templates by using conditional statements, loops, and other logical constructs. These structures are essential for creating dynamic and flexible templates in your ShopWired store.
Common control structures
if elseif else
The if
statement is used to execute a block of code only if a specified condition is true. The elseif
and else
statements provide additional logic to handle other cases when the initial condition is not met.
{% if global.customer.first_name %}
<p>Welcome, {{ global.customer.first_name }}!</p>
{% elseif not global.customer.id %}
<p>Welcome, please continue to set up your account.</p>
{% else %}
<p>Welcome! Please register or log in.</p>
{% endif %}
This example checks if a visitor is logged in and has set a first name on their account. If not, it checks if they are logged in and displays different messages based on their status.
for
The for
loop is used to iterate over items in an array or object. This is useful for displaying lists of products, categories, or any other data sets in your ShopWired templates.
{% for product in products %}
<div class="product">
<h2>{{ product.title }}</h2>
<p>Price: {{ product.price }}</p>
</div>
{% else %}
<p>No products available.</p>
{% endfor %}
In this example, the loop iterates over each product
in the products
array and displays its name and price. The else
clause is executed if the array is empty.
set
The set
tag is used to assign values to variables within your template. This is often combined with other control structures to store and manipulate data.
{% set discount = 0.1 %}
<p>The discount is {{ discount * 100 }}%.</p>
This example sets a variable discount
to 0.1
(representing 10%) and then displays the discount as a percentage.
include
The include
statement allows you to include the contents of one template into another. This is useful for reusing common elements like headers, footers, or sidebars.
{% include 'header.twig' %}
<p>Welcome to our store!</p>
{% include 'footer.twig' %}
This example includes the contents of header.twig
at the beginning and footer.twig
at the end of the template.
with
The with
block is used to isolate variables within a specific block of code, preventing them from affecting the rest of the template.
{% with %}
{% set special_offer = "20% off!" %}
<p>Special Offer: {{ special_offer }}</p>
{% endwith %}
In this example, the variable special_offer
is only available within the with
block.
spaceless
The spaceless
tag is used to remove whitespace between HTML tags in the final output. This can help reduce the size of the HTML file, making your website faster to load.
{% spaceless %}
<div>
<span>Item 1</span>
<span>Item 2</span>
</div>
{% endspaceless %}
In this example, all whitespace between the <div>
and <span>
tags will be removed in the final output.
macro
Although typically associated with macros, the macro
tag can also function as a control structure, enabling you to define reusable code blocks that can be called with different parameters.
{% macro button(text, url) %}
<a href="{{ url }}" class="button">{{ text }}</a>
{% endmacro %}
{{ button('Shop Now', '/shop') }}
This macro creates a button with a link and can be reused anywhere in your template with different text
and url
values.