Twig Functions

Herbie is using Twig Functions for generating content in layout and content files. In addition to the built-in functions of Twig itself, the following functions are available.

css_add

Registers a CSS asset for output by css_out. Use raw=true to include the CSS inline.

Parameter Type Description Default
paths string|array

One or more paths to CSS resources.

[]
attribs array

The HTML attributes passed as an associative array.

[]
group string|null

The asset group name.

null
raw bool

If true, paths is treated as inline CSS content and rendered in a style tag instead of being referenced by URL.

false
position int

The sort position of the asset within its group.

1
[return] void
Example:
{{ css_add("@theme/css/styles.css", {class: "styles"}, "default", false, 1) }}

css_classes

Returns a string with CSS classes, to be used within an HTML attribute, for example the html or body tag. This allows to address any page, theme, layout or language via CSS selector. Such a string looks like: "page-blog theme-default layout-blog language-en"

[return] Description
string

The string with CSS classes.

Example:
{{ css_classes() }}

css_out

Returns the HTML markup for the registered CSS assets of a group.

Parameter Type Description Default
group string|null

The asset group to output. If null, the default group is used.

null
timestamp bool

If true, a timestamp query parameter is appended to asset URLs.

false
[return] string

The HTML markup for the CSS assets

Example:
{{ css_out("default", true) }}

file_url

Returns the public URL for a file. A path below the media URL (urls.media, by default /media/...) is served by Herbie from @media: types a browser can display are opened inline, everything else is downloaded, and the option download appends ?download to force a download. Every other path is relative to the web root and served by the web server. Base URL and script name are added automatically. External URLs and anchors are returned unchanged.

Parameter Type Description Default
path string

The public path to the file, for example /media/manual.pdf or /logo.png.

options array

Options as an associative array. download (bool) forces a download, it has an effect on media files only.

[]
[return] string

The public URL

Example:
{{ file_url("/media/manual.pdf", {download: true}) }}

html_attributes

Renders an associative array of HTML attributes into an escaped string. null and false values are omitted; true renders a boolean attribute. Attribute names are escaped with the html_attr_relaxed strategy.

Parameter Type Description Default
attribs array

The HTML attributes passed as an associative array.

[]
strategy string

The Twig escaping strategy used for attribute values, for example html_attr, html, js, url, or css.

html_attr
[return] string

The rendered HTML attributes

Example:
{{ html_attributes({class: "btn", disabled: true, hidden: false, title: null}) }}

image

Returns an HTML image element for a file. A path below /media/ is served by Herbie from @media, every other path is relative to the web root. See file_url(). No image processing is performed.

Parameter Type Description Default
src string

The public path to the image file, for example /media/portrait.jpg or /logo.png.

attribs array

The HTML attributes passed as an associative array.

[]
template string

The aliased path to the template file.

@snippet/image.twig
[return] string

The HTML image element

Example:
{{ image("/media/portrait.jpg", {width: 100, height: 200, alt: "Portrait", class: "photo"}) }}

js_add

Registers a JavaScript asset for output by js_out. Use raw=true to include the script inline.

Parameter Type Description Default
paths string|array

One or more paths to JavaScript resources.

[]
attribs array

The HTML attributes passed as an associative array.

[]
group string|null

The asset group name.

null
raw bool

If true, paths is treated as inline JavaScript content and rendered in a script tag instead of being referenced by URL.

false
position int

The sort position of the asset within its group.

1
[return] void
Example:
{{ js_add("@theme/js/script.js", {class: "scripts"}, "default", false, 1) }}

js_out

Returns the HTML markup for the registered JavaScript assets of a group.

Parameter Type Description Default
group string|null

The asset group to output. If null, the default group is used.

null
timestamp bool

If true, a timestamp query parameter is appended to asset URLs.

false
[return] string

The HTML markup for the JavaScript assets

Example:
{{ js_out("default", true) }}

Returns an HTML link to a file in @media that forces a download (/media/...?download). Shortcut for link_file() with a download URL, see file_url().

Parameter Type Description Default
path string

The path to the file relative to @media. The prefix /media/ is optional.

label string

The link label. If empty, the file basename is used.

attribs array

The HTML attributes passed as an associative array.

[]
show_info bool

If true, file extension and size are appended to the label.

false
template string

The aliased path to the template file.

@snippet/link_download.twig
[return] string

The rendered HTML

Example:
{{ link_download("dummy.pdf", "My dummy file", {class: "link-file"}, true) }}

Returns an HTML link to a file. The path is resolved like in file_url(); use link_download() to force a download of a media file.

Parameter Type Description Default
path string

The public path to the file, for example /media/download.pdf or /files/info.pdf.

label string

The link label. If empty, the file basename is used.

attribs array

The HTML attributes passed as an associative array.

[]
show_info bool

If true, file extension and size are appended to the label.

false
template string

The aliased path to the template file.

@snippet/link_file.twig
[return] string

The rendered HTML

Example:
{{ link_file("media/download.pdf", "Download", {class:"download"}, true) }}

Returns an HTML mailto link.

Parameter Type Description Default
email string

The email address.

label string|null

The link label. If null, the email address is used.

null
attribs array

The HTML attributes passed as an associative array.

[]
template string

The aliased path to the template file.

@snippet/link_mail.twig
[return] string

The rendered HTML

Example:
{{ link_mail("me@example.com", "John Doe", {class: "link-page"}, "@snippet/link_mail.twig") }}

Returns an HTML link for a given route or URL. Use url_rel() or url_abs() when you only need the URL string.

Parameter Type Description Default
route string

The route or the URL to a page.

label string

The link label.

attribs array

The HTML attributes passed as an associative array.

[]
template string

The aliased path to the template file.

@snippet/link_page.twig
[return] string

The rendered HTML

Example:
{{ link_page("doc/contents/variables", "Content variables", {class: "link-page"}) }}

Returns a text-based ASCII tree of pages.

Parameter Type Description Default
route string

The route of the branch to render. If empty, the full page tree is used.

""
depth int

The maximum depth level of the tree (-1 = unlimited).

-1
hidden bool

If true, hidden pages are included.

false
[return] string

The rendered ASCII tree

Example:
{{ menu_ascii_tree("doc", 2, true) }}

Returns a breadcrumb navigation for the current page trail.

Parameter Type Description Default
delimiter string

The delimiter passed to the template.

""
home array

Optional home item as [route, label]. Pass an empty array to omit it.

[]
reverse bool

If true, the breadcrumb items are reversed.

false
template string

The aliased path to the template file.

@snippet/menu_breadcrumb.twig
[return] string

The rendered HTML

Example:
{{ menu_breadcrumb(" / ", ["", "Homepage"], false, "@snippet/menu_breadcrumb.twig") }}

Returns a paginated list of pages. The current page is read from the "page" query parameter by default.

Parameter Type Description Default
where array|string

One or more where conditions passed to the query builder.

""
order string

The order field(s) passed to the query builder.

""
shuffle bool

If true, the pages are shuffled before querying.

false
limit int

The number of items per page.

10
template string

The aliased path to the template file.

@snippet/menu_list.twig
[return] string

The rendered HTML

Example:
{{ menu_list("type=blog", "-title", false, 10, "@snippet/menu_list.twig") }}

Returns a pager with links to the previous and next page within the current page branch.

Parameter Type Description Default
prefix string

Limits the pager to pages whose route starts with this prefix.

prev_label string

The label for the previous link. If empty, the page menu title is used.

next_label string

The label for the next link. If empty, the page menu title is used.

prev_icon string

The icon markup for the previous link.

next_icon string

The icon markup for the next link.

class string

The CSS class name passed to the template.

pager
template string

The aliased path to the template file.

@snippet/menu_pager.twig
[return] string

The rendered HTML

Example:
{{ menu_pager("doc/contents/", "Previous", "Next", "<", ">", "pager", "@snippet/menu_pager.twig") }}

Returns an HTML sitemap as a nested unordered list. This is an alias for menu_tree() with sitemap as the default CSS class.

Parameter Type Description Default
route string

The route of the branch to render. If empty, the full page tree is used.

""
depth int

The maximum depth level of the tree (-1 = unlimited).

-1
hidden bool

If true, hidden pages are included.

false
class string

The CSS class for the root list element.

sitemap
[return] string

The rendered HTML

Example:
{{ menu_sitemap("doc", 1, false, "sitemap") }}

Returns an HTML navigation tree as a nested unordered list.

Parameter Type Description Default
route string

The route of the branch to render. If empty, the full page tree is used.

""
depth int

The maximum depth level of the tree (-1 = unlimited).

-1
hidden bool

If true, hidden pages are included.

false
class string

The CSS class for the root list element.

menu
[return] string

The rendered HTML

Example:
{{ menu_tree("doc", 1, false, "menu") }}

page_title

Returns the assembled page title for the current page trail.

Parameter Type Description Default
delimiter string

The delimiter between title segments.

" / "
site_title string

The website title. If not empty, it is prepended before reversal.

""
root_title string

The title used on the root/start page instead of the normal page title.

""
reverse bool

If true, the assembled title segments are reversed.

false
[return] string

The page title

Example:
{{ page_title(delimiter=" / ", site_title="My Blog", root_title="Welcome to My Blog", reverse=true) }}

query

Creates a query builder for iterable data. The returned builder can filter, sort, limit, and paginate the data.

Parameter Type Description Default
data iterable

The data to be queried.

[]
[return] QueryBuilder

The query builder

Example:
{{ query(data).where("type=page").where("title*=news").limit(3).order("-date").all() }}

snippet

Renders a Twig snippet template and returns the result. Automatic escaping is disabled for all escaping strategies.

Parameter Type Description Default
path string

The aliased or relative path to the snippet template.

context array

The variables passed to the snippet as an associative array.

[]
[return] string

The rendered snippet

Example:
{{ snippet("@site/custom-snippet.twig", {foo: "bar"}) }}

translate

Translates a message using the configured translator and current locale.

Parameter Type Description Default
category string

The translation category.

message string

The message to translate.

params array

The placeholder parameters passed to the translator.

[]
[return] string

The translated message

Example:
{{ translate("app", "Plugin {plugin} enabled but not found!", {plugin: "dummy"}) }}

twigify

Renders a string as a Twig template in the current context and returns the result.

Parameter Type Description Default
content string

The string to render as a Twig template.

[return] string

The rendered string

Example:
{{ twigify("Hello {{ page.title }}") }}

url_abs

Returns the absolute URL for a given route. Use this when you only need the URL string, not a complete HTML link element.

Parameter Type Description Default
route string

The route to a page. Must be given, an empty route is not resolved.

required
[return] string

The absolute URL.

Example:
{{ url_abs("doc/contents/variables") }}

url_rel

Returns the relative URL for a given route. Use this when you only need the URL string, not a complete HTML link element.

Parameter Type Description Default
route string

The route to a page. Must be given, an empty route is not resolved.

required
[return] string

The relative URL.

Example:
{{ url_rel("doc/contents") }}

Built-in Functions

In addition to the functions mentioned above, Twig's built-in functions are of course also available. Please note that some of the functions may require the installation of additional Composer packages. See the official Twig documentation for the complete list and examples.