Template Syntax
Ubex folosește Pongo2, un motor de template-uri puternic, în stilul Django/Jinja2. Acesta vă oferă control deplin asupra modului în care datele circulă prin workflow-urile dumneavoastră.
Variabile de bază
Folosiți acolade duble pentru a afișa variabile:
{{ variable_name }}
{{ user.name }}
{{ items[0].title }}
Filtre
Transformați valorile folosind filtre cu operatorul pipe |:
{{ name | upper }}
{{ text | lower }}
{{ price | floatformat:2 }}
{{ items | length }}
{{ description | truncatechars:100 }}
{{ html_content | safe }}
{{ list | join:", " }}
{{ date | date:"2006-01-02" }}
{{ value | default:"N/A" }}
Filtre uzuale
| Filtru | Descriere | Exemplu |
|---|---|---|
upper |
Text cu majuscule | {{ name | upper }} |
lower |
Text cu minuscule | {{ name | lower }} |
title |
Capitalizare de tip titlu | {{ name | title }} |
length |
Numărare elemente/caractere | {{ items | length }} |
default |
Valoare de rezervă | {{ value | default:"none" }} |
join |
Unire elemente dintr-un array | {{ tags | join:", " }} |
first |
Primul element | {{ items | first }} |
last |
Ultimul element | {{ items | last }} |
truncatechars |
Limitare caractere | {{ text | truncatechars:50 }} |
safe |
Randare HTML | {{ html | safe }} |
floatformat |
Formatare zecimale | {{ price | floatformat:2 }} |
Condiții
Folosiți {% if %} pentru logică condițională:
{% if user.is_premium %}
Welcome back, premium member!
{% elif user.is_trial %}
Your trial ends in {{ user.trial_days }} days.
{% else %}
Sign up for premium features.
{% endif %}
Operatori de comparație
{% if count > 10 %}...{% endif %}
{% if status == "active" %}...{% endif %}
{% if name != "" %}...{% endif %}
{% if score >= 80 %}...{% endif %}
{% if items and items | length > 0 %}...{% endif %}
{% if not is_disabled %}...{% endif %}
Operatori logici
{% if is_admin and is_active %}...{% endif %}
{% if is_free or is_trial %}...{% endif %}
{% if not is_blocked %}...{% endif %}
Bucle
Folosiți {% for %} pentru a itera peste array-uri:
{% for item in items %}
- {{ item.name }}: {{ item.value }}
{% endfor %}
Variabile de buclă
În interiorul unei bucle, aveți acces la variabile speciale:
{% for user in users %}
{{ forloop.Counter }}: {{ user.name }}
{% if forloop.First %}(first item){% endif %}
{% if forloop.Last %}(last item){% endif %}
{% endfor %}
| Variabilă | Descriere |
|---|---|
forloop.Counter |
Iterația curentă (indexată de la 1) |
forloop.Counter0 |
Iterația curentă (indexată de la 0) |
forloop.First |
True la prima iterație |
forloop.Last |
True la ultima iterație |
forloop.Revcounter |
Iterații rămase |
Bucle goale
Gestionați array-urile goale cu eleganță:
{% for item in items %}
{{ item.name }}
{% empty %}
No items found.
{% endfor %}
Exemple practice
Construirea unui prompt cu context
You are analyzing customer feedback.
{% for review in reviews %}
Review #{{ forloop.Counter }}:
Rating: {{ review.rating }}/5
Comment: {{ review.text | truncatechars:200 }}
{% endfor %}
Summarize the overall sentiment and key themes.
Corp condițional pentru o cerere API
{
"user": "{{ user.email }}",
"plan": "{{ plan | default:"free" }}",
{% if include_metadata %}
"metadata": {
"source": "{{ source }}",
"timestamp": "{{ timestamp }}"
}
{% endif %}
}
Conținut dinamic de e-mail
Hi {{ user.name | title }},
{% if order.status == "shipped" %}
Great news! Your order #{{ order.id }} has shipped.
Tracking: {{ order.tracking_number }}
{% elif order.status == "processing" %}
Your order #{{ order.id }} is being prepared.
{% else %}
Your order #{{ order.id }} is confirmed.
{% endif %}
Items ordered:
{% for item in order.items %}
- {{ item.quantity }}x {{ item.name }} - ${{ item.price | floatformat:2 }}
{% endfor %}
Total: ${{ order.total | floatformat:2 }}
Generarea interogărilor SQL
SELECT * FROM users
WHERE status = 'active'
{% if department %}
AND department = '{{ department }}'
{% endif %}
{% if min_score %}
AND score >= {{ min_score }}
{% endif %}
ORDER BY created_at DESC
LIMIT {{ limit | default:100 }}
Sfaturi
- Folosiți
{{ variable | default:"fallback" }}pentru a gestiona valorile lipsă - Folosiți
{% if variable %}pentru a verifica dacă o variabilă există și nu este goală - Filtrele pot fi înlănțuite:
{{ name | lower | truncatechars:20 }} - Spațiile din interiorul
{{ }}și{% %}sunt ignorate