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 |
false |
| position | int | The sort position of the asset within its group. |
1 |
| [return] | void | ||
{{ 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. |
{{ 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 |
|
{{ 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 |
|
| options | array | Options as an associative array. |
[] |
| [return] | string | The public URL |
|
{{ 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 |
| [return] | string | The rendered HTML attributes |
|
{{ 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 |
|
| 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 |
|
{{ 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 |
false |
| position | int | The sort position of the asset within its group. |
1 |
| [return] | void | ||
{{ 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 |
|
{{ js_out("default", true) }}
link_download
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 |
|
| 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 |
|
{{ link_download("dummy.pdf", "My dummy file", {class: "link-file"}, true) }}
link_file
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 |
|
| 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 |
|
{{ link_file("media/download.pdf", "Download", {class:"download"}, true) }}
link_mail
Returns an HTML mailto link.
| Parameter | Type | Description | Default |
| 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 |
|
{{ link_mail("me@example.com", "John Doe", {class: "link-page"}, "@snippet/link_mail.twig") }}
link_page
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 |
|
{{ link_page("doc/contents/variables", "Content variables", {class: "link-page"}) }}
menu_ascii_tree
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 |
|
{{ menu_ascii_tree("doc", 2, true) }}
menu_breadcrumb
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 |
|
{{ menu_breadcrumb(" / ", ["", "Homepage"], false, "@snippet/menu_breadcrumb.twig") }}
menu_list
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 |
|
{{ menu_list("type=blog", "-title", false, 10, "@snippet/menu_list.twig") }}
menu_pager
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 |
|
{{ menu_pager("doc/contents/", "Previous", "Next", "<", ">", "pager", "@snippet/menu_pager.twig") }}
menu_sitemap
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 |
|
{{ menu_sitemap("doc", 1, false, "sitemap") }}
menu_tree
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 |
|
{{ 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 |
|
{{ 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 |
|
{{ 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 |
|
{{ 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 |
|
{{ 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 |
|
{{ 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. |
|
{{ 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. |
|
{{ 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.