Twig Reference
All available Twig functions, filters, and helpers for building themes and templates in ZephyrPHP.
Collection & Entry Functions
These functions let you query and display content from your collections in templates.
| Function | Description | Example |
collection(slug, options) |
Fetch paginated entries from a collection |
{% set posts = collection('blog_posts', {per_page: 10, page: 1, sort_by: 'id', sort_dir: 'DESC'}) %}
{% for item in posts.data %}
{{ item.title }}
{% endfor %}
|
entry(slug, identifier) |
Fetch a single entry by slug or ID |
{% set post = entry('blog_posts', 'hello-world') %}
{{ post.title }}
|
collection_meta(slug) |
Get collection metadata (name, slug, url_prefix, has_slug) |
{% set meta = collection_meta('blog_posts') %}
{{ meta.url_prefix }}
|
entry_query(slug) |
Get a fluent query builder for advanced queries |
{% set query = entry_query('products') %}
|
Collection Options
| Option | Type | Default | Description |
per_page | int | 10 | Entries per page |
page | int | 1 | Current page number |
sort_by | string | 'id' | Field to sort by |
sort_dir | string | 'DESC' | Sort direction (ASC or DESC) |
Collection Return Value
The collection() function returns a paginated result with these properties:
| Property | Description |
data | Array of entries for the current page |
current_page | Current page number |
last_page | Total number of pages |
per_page | Items per page |
total | Total number of entries |
URL & Navigation Functions
| Function | Description | Example |
admin_url(path) |
Generate admin panel URL (respects ADMIN_PATH env) |
{{ admin_url('collections') }} |
login_url() |
Get the login page URL |
{{ login_url() }} |
logout_url() |
Get the logout URL |
{{ logout_url() }} |
Theme & Section Functions
| Function | Description |
theme_settings() | Get global theme settings (colors, fonts, etc.) |
theme_config() | Get the active theme configuration from theme.json |
has_sections(pageTemplate) | Check if a page has sections configured |
render_sections(pageTemplate) | Render all sections for a page |
asset(path) | Get URL to a theme asset file |
assets_head() | Output CSS and head scripts for the theme |
assets_footer() | Output footer scripts for the theme |
SEO Functions
Available when SEO is enabled on a collection:
| Function | Description |
seo_meta(entry, collectionSlug) | Generate meta description and keywords tags |
og_tags(entry, collectionSlug) | Generate Open Graph meta tags for social sharing |
json_ld(entry, collectionSlug) | Generate JSON-LD structured data |
seo_title(entry, collectionSlug) | Get SEO-optimized page title |
Security Functions
| Function | Description |
csrf_field() | Output a hidden CSRF token field for forms. Always use with |raw |
csrf_token() | Get the raw CSRF token string |
csp_nonce() | Get the Content Security Policy nonce for inline scripts |
Utility Functions
| Function | Description |
cms_can(permission) | Check if current user has a permission |
notification_count() | Get unread notification count |
current_locale() | Get current language/locale code |
available_locales() | Get all active languages |
render_collection_form(slug, options) | Render a public form for submitting entries to a collection |
Common Twig Filters
Standard Twig filters you'll use frequently:
| Filter | Example | Description |
raw | {{ content|raw }} | Output HTML without escaping |
striptags | {{ content|striptags }} | Remove HTML tags |
slice | {{ text|slice(0, 120) }} | Truncate text to length |
date | {{ item.created_at|date('M d, Y') }} | Format dates |
default | {{ title|default('Untitled') }} | Fallback value if empty/null |
url_encode | {{ value|url_encode }} | URL-encode a string |
lower | {{ name|lower }} | Convert to lowercase |
upper | {{ name|upper }} | Convert to uppercase |
capitalize | {{ name|capitalize }} | Capitalize first letter |
length | {{ items|length }} | Get array/string length |
join | {{ tags|join(', ') }} | Join array items with separator |
split | {{ csv|split(',') }} | Split string into array |
replace | {{ text|replace({'_': ' '}) }} | Replace substrings |
number_format | {{ price|number_format(2) }} | Format numbers |
Template Variables
Variables available in your page templates:
Page Templates
| Variable | Description |
page.title | The page title |
page.slug | The page URL route pattern (e.g. /blogs) |
page.url | The actual request URL |
page.template | Template name |
params | Route parameters (for dynamic pages) |
theme_settings | Global theme settings |
Section Templates
| Variable | Description |
section.settings | All settings values for this section instance |
section.blocks | Array of blocks added to this section |
section.id | Unique section instance ID |
section.type | Section type slug |
Twig Syntax Quick Reference
| Syntax | Purpose | Example |
{{ }} | Output a value | {{ item.title }} |
{% %} | Execute logic (if, for, set) | {% if items|length > 0 %} |
{# #} | Comments (not rendered) | {# This is a comment #} |