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.

FunctionDescriptionExample
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

OptionTypeDefaultDescription
per_pageint10Entries per page
pageint1Current page number
sort_bystring'id'Field to sort by
sort_dirstring'DESC'Sort direction (ASC or DESC)

Collection Return Value

The collection() function returns a paginated result with these properties:

PropertyDescription
dataArray of entries for the current page
current_pageCurrent page number
last_pageTotal number of pages
per_pageItems per page
totalTotal number of entries

URL & Navigation Functions

FunctionDescriptionExample
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

FunctionDescription
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:

FunctionDescription
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

FunctionDescription
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

FunctionDescription
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:

FilterExampleDescription
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

VariableDescription
page.titleThe page title
page.slugThe page URL route pattern (e.g. /blogs)
page.urlThe actual request URL
page.templateTemplate name
paramsRoute parameters (for dynamic pages)
theme_settingsGlobal theme settings

Section Templates

VariableDescription
section.settingsAll settings values for this section instance
section.blocksArray of blocks added to this section
section.idUnique section instance ID
section.typeSection type slug

Twig Syntax Quick Reference

SyntaxPurposeExample
{{ }}Output a value{{ item.title }}
{% %}Execute logic (if, for, set){% if items|length > 0 %}
{# #}Comments (not rendered){# This is a comment #}