Zid Themes Docs
  • Getting started
  • Twig reference
  • Theme file structure
  • zid-theme package
  • Theme Templates and Data
    • layout.twig
  • Store language and currency
  • Home template modules
  • Product template
  • Cart template
  • Settings Schema
    • text
    • number
    • textarea
    • select
    • radios
    • checkbox
    • checkboxes
    • range
    • color
    • image
    • product
    • category
    • category products
    • list
    • fieldset
  • Data reference
    • locals
    • store
    • cart
    • product
    • products list
    • category
    • categories list
    • session
    • customer
    • faqs
    • blogs
    • blog
    • page
    • main menu
    • request
    • orders
    • addresses
    • store payment methods
    • store shipping methods
    • store banks
    • asset_url
    • header_meta_tags
  • Zid-api Library
    • Product
    • Category
    • Cart
    • Blog
    • Customer
    • Settings
  • Upload themes and more
Powered by GitBook
On this page
  • Syntax
  • Tags
  • Available tags:
  • Extends and block tags
  • Include
  • For-loops
  • If-else
  • Set
  • Available filters:
  • Available functions:
  • Twig resources

Was this helpful?

Twig reference

PreviousGetting startedNextTheme file structure

Last updated 3 years ago

Was this helpful?

zid theme uses under the hood, checkout its full documentation to get started building zid themes.

Syntax

Like other template engines, Twig has two main ways interacts with variables and injected data. The first one is used to denote an output from a variable by using a double curly braces {{ }}. Whereas the curly brace with percentage {% %}, is used to construct the logic and control flow.

Tags

In twig, most tags including if-else, loops, and blocks need to be declared inside start & end tags. There are other tags which use single tag, such as extends tag and include function.

{% starg_tag %} 
    code block
{% end_tag %} 

The extends tag is used to extend another twig file to use as a layout or base code. Adding on, the content block defined in home.twig will replace the content block declared in layout.twig

Available tags:

Zid allows only 6 Twig tags:

  • extends

  • block

  • include

  • for-loop

  • if-else

  • set

Extends and block tags

The powerful part of Twig is template inheritance, whcich allows you to define a base code that containts all you website elements, and allows you as well to define blocks that can be overridden from child templates

home.twig
{% extends "layout.twig" %}

{% block content %}
    <h1>
        This content will replace the content 
        block in layout.twig
    </h1>
{% endblock %}

Here in layout.twig, we can see there are couple of blocks defined that will be replaced in the extended file from the child file. For example, the block content in layout.twig line 20 will be replaced with the block content from home.twig. And the same inheritance applied to all other block, if any declared.

layout.twig
<html lang="">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>{% block title %}{% endblock %} | osama</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">

    {% block head %} {% endblock %}

</head>

<body>

    <div class="app">
        {% block header %}{% endblock %}
        {% block content %}{% endblock %}
        {% block footer %}{% endblock %}
    </div>

    {{ zidapi_script|raw }}
    <script type="text/javascript" src="{{ asset_url ~ 'main.js' }}"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>

Include

include function is used to include a content of a file inside a block tag. Think of include function as a copy-paste operation, you copy the content of file foo into bar file

bar.twig
{% block header %} 
		{{ include('foo.twig') }} 
{% endblock %}

For-loops

{% for product in products %}
		<div class="card shadow">
				<a href="/products/{{ product.id }}"></a>
	  </div>
{% endfor %}

If-else

if ,elseif, else , endif are used to assign values to variables, which are declared between the double curly brackets: {{ }}

// example 1
{% if customer %}
		<div>
				{{ customer.name }}
		</div>
{% endif %}

// example 2
{% if product.stock > 10 %}
   Available
{% elseif product.stock > 0 %}
   Only {{ product.stock }} left!
{% else %}
   Sold-out!
{% endif %}

Set

set as a start tag is used inside blocks to assign values to variables

{% set foo = 'bar' %}
{{ foo }} /* foo will be 'bar' */

Available filters:

Zid allows only 3 Twig filters:

['escape','length', 'raw']

Available functions:

Zid allows only 2 Twig functions:

['range', 'include']

Twig resources

Note products array here is passed from the server

For more examples and in-depth documentation, visit docs page:

Twig template engine
(See data section for more information on data)
Twig
Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine
Logo