تخطَّ إلى المحتوى

البناء باستخدام واجهة

أساسيات Jinja

غير مترجمة بعدهذه الصفحة متوفرة بالإنجليزية فقط حتى الآن، وتُعرض أدناه بالإنجليزية بدل حجبها.

Wajha templates use Jinja-style syntax. If you have written Django, Jinja2, Twig or Liquid templates, most of this will be familiar — but read What Wajha Does Not Support before porting anything, because the differences are real.

Three kinds of delimiter

{{ ... }}   {# print an expression #}
{% ... %}   {# a statement: if, for, block, include #}
{# ... #}   {# a comment, never rendered #}

Printing values

<h1>{{ product.name }}</h1>
<p>{{ store.name }} — {{ store.country }}</p>
<span>{{ product.price.formatted }}</span>

Dotted access works on maps and lists:

{{ product.images[0].url }}
{{ settings.hero_heading }}

Everything you print is HTML-escaped automatically. A product named <script>alert(1)</script> renders as visible text, not as a script. You cannot turn this off — see the security model.

Conditionals

{% if product.in_stock %}
  <button>{{ t.cart.add }}</button>
{% elif product.restock_expected %}
  <p>{{ t.product.restocking }}</p>
{% else %}
  <p>{{ t.product.sold_out }}</p>
{% endif %}

Operators: ==, !=, <, >, <=, >=, and, or, not, in.

{% if product.tags and 'new' in product.tags %}
  <span class="badge">{{ t.product.new }}</span>
{% endif %}

Empty strings, empty lists, 0 and null are all falsy.

Loops

{% for product in products %}
  <article>{{ product.name }}</article>
{% endfor %}

Loop state is available through forloop. Note the capitalised field names — this is one of the places the syntax differs from Python-side Jinja:

{% for image in product.images %}
  <img src="{{ image.url }}"
       alt="{{ image.alt }}"
       loading="{% if forloop.First %}eager{% else %}lazy{% endif %}">
{% endfor %}
FieldMeaning
forloop.Counter1-based position
forloop.Counter00-based position
forloop.Firstfirst iteration
forloop.Lastlast iteration
forloop.Revcounterpositions remaining

An empty collection is handled with {% empty %} — Django's name for it, not Jinja's {% else %}:

{% for product in products %}
  {% include 'partials/product-card.jinja' %}
{% empty %}
  <p>{{ t.catalogue.empty }}</p>
{% endfor %}

Filters

Filters transform a value with | and chain left to right. An argument follows a colon, not parentheses — this is the single most common thing to trip over coming from Jinja:

{{ product.description|truncatechars:160 }}
{{ product.name|upper }}
{{ collection|length }}
{{ product.subtitle|default:'—' }}

{{ value | truncate(160) }} is Jinja and does not compile here.

The full list is in the Filters Reference.

Your theme's own strings

Text your theme displays comes from locales/ar.json and locales/en.json, and is read as data under t — not through a filter:

<button>{{ t.cart.add }}</button>
<a href="/">{{ t.not_found.home }}</a>

The locale files are nested JSON, and the active language is already chosen:

{ "cart": { "add": "Add to cart" }, "not_found": { "home": "Back to the shop" } }

A key that does not exist renders as an empty string rather than an error, so the platform checks your locale files instead: a theme whose ar.json and en.json do not have exactly the same keys is refused when it is loaded.

Assets

Link your theme's own files through theme.asset_base, which includes a fingerprint of the theme so the files can be cached permanently and still update the moment you publish:

<link rel="stylesheet" href="{{ theme.asset_base }}/theme.css">

Money, date and number filters — money, date, number — arrive with product pages, and are documented on Money, Dates & Numerals ahead of time because they are part of the contract. Until then, print the pre-formatted formatted field every amount carries.

Template inheritance

Every template extends the layout and fills its blocks:

{# templates/product.jinja #}
{% extends 'layout.jinja' %}

{% block content %}
  <article class="product">
    <h1>{{ product.name }}</h1>
    {% include 'partials/gallery.jinja' %}
    <p>{{ product.price.formatted }}</p>
  </article>
{% endblock %}

{% extends %} must be the first statement in the file. A block defined in the layout can be overridden by any template that extends it, and {{ block.Super }} renders the parent's version inside the override:

{% block title %}{{ t.products.title }} — {{ block.Super }}{% endblock %}

It is block.Super, with a capital S. The lowercase block.super that Django uses does not raise an error — it renders nothing, silently, and the parent's content disappears from the page. If a block you extended has gone blank, check this first.

Includes and partials

{% include 'partials/product-card.jinja' %}

An include sees the current context. To pass something explicitly, set it first:

{% for item in cart.lines %}
  {% with product = item.product %}
    {% include 'partials/line-item.jinja' %}
  {% endwith %}
{% endfor %}

Include paths are relative to templates/ and must stay inside your theme. A path with .. in it fails at compile time, not at render time.

Variables

{% with columns=settings.grid_columns|default:4 %}
  <ul style="--columns: {{ columns }}">
    ...
  </ul>
{% endwith %}

{% set %} also works, and lasts until the end of the enclosing block:

{% set heading = page.title|default:store.name %}

Whitespace

Add a hyphen inside a delimiter to trim adjacent whitespace. It works on both tags and prints — {%- ... -%} and {{- ... -}}:

<ul>
  {%- for tag in product.tags -%}
    <li>{{ tag }}</li>
  {%- endfor -%}
</ul>

Worth doing inside <pre>, inside <title>, and in JSON-LD blocks where stray whitespace is a parse concern. Elsewhere it is noise.