Twig Extensions
Introduction
Twig Extensions are functions, filters and more that offer special functionality to increase usability of view scripts.
Following an overview of some Twig Extensions:
renderrender_esicontrollerassetcsrf_tokenpathabsolute_urltranslatortransurlrelative_path
In addition to the standard Twig extensions, Pimcore adds some additional powerful extensions.
Pimcore Twig Extensions
All Twig extension functions are described below in detail, the following tables give just a short overview of all available extensions.
Functions Overview
| Extension | Description |
|---|---|
pimcorecache | Simple in-template caching functionality |
pimcore_cache() (deprecated) | Simple in-template caching functionality (deprecated legacy version) |
pimcore_device() | Helps implementing adaptive designs |
pimcore_placeholder() | Adding and embedding custom placeholders, e.g. for special header tags, etc. |
pimcore_head_link() | Embedding / managing referenced stylesheets (alternative to assets()) |
pimcore_head_meta() | Managing your <meta> elements in your HTML document |
pimcore_head_script() | Managing your <scripts> elements |
pimcore_head_style() | Managing inline styles (counterpart to headLink() for inline styles) |
pimcore_head_title() | Create and store the HTML document's <title> for later retrieval and output |
pimcore_inc() | Use this function to directly include a Pimcore document |
pimcore_inline_script | Managing inline scripts (counterpart to headScript() for inline scripts) |
pimcore_build_nav(), pimcore_render_nav(), pimcore_nav_renderer() | Embed and build navigations based on the document structure |
pimcore_url() | An alternative to url() and path() |
pimcore_website_config() | Fetch website settings or specific setting (first param: key) for the current site |
pimcore_image_thumbnail() | Returns a path to a given thumbnail on image |
pimcore_image_thumbnail_html() | Returns html for displaying the thumbnail image |
pimcore_supported_locales() | Use this function to get a list of supported locales |
pimcore_document() | Load a document by ID |
pimcore_document_by_path() | Load a document by path |
pimcore_site() | Get the current site |
pimcore_asset() | Load an asset by ID |
pimcore_asset_by_path() | Load an asset by path |
pimcore_object() | Load a data object by ID |
pimcore_object_by_path() | Load a data object by path |
pimcore_user() | Load a user by ID |
pimcore_document_wrap_hardlink() | Wrap a document for hardlink context |
pimcore_object_classificationstore_group() | Get classification store group definition by ID |
pimcore_object_classificationstore_get_field_definition_from_json() | Get classification store field definition from JSON |
pimcore_object_brick_definition_key() | Get objectbrick definition by key |
pimcore_element_tags() | Get assigned element tags |
pimcore_iterate_block() | Iterate over a Block editable's elements |
Tags Overview
| Tag | Description |
|---|---|
pimcorecache / endpimcorecache | In-template caching block |
pimcoreblock / endpimcoreblock | Block editable iteration tag |
Tests Overview
| Test | Description |
|---|---|
instanceof(classname) | Checks if an object is an instance of a given class |
pimcore_asset | Checks if object is instanceof Asset |
pimcore_data_object | Checks if object is instanceof DataObject\Concrete |
pimcore_document | Checks if object is instanceof Document |
Tests
Pimcore adds Twig tests for evaluating boolean conditions, e.g.
{# using 'instanceof' checks if object is instanceof provided classname #}
{% if product is instanceof('App\\Model\\Product\\Car') %}
...
{% endif %}
{# using 'pimcore_data_object' checks if object is instanceof \Pimcore\Model\DataObject\Concrete #}
{% if product is pimcore_data_object %}
...
{% endif %}
The following table gives an overview of all available tests:
| Test | Description |
|---|---|
instanceof(classname) | Checks if an object is an instance of a given class |
pimcore_asset | Checks if object is instanceof Asset |
pimcore_asset_archive | Checks if object is instanceof Asset\Archive |
pimcore_asset_audio | Checks if object is instanceof Asset\Audio |
pimcore_asset_document | Checks if object is instanceof Asset\Document |
pimcore_asset_folder | Checks if object is instanceof Asset\Folder |
pimcore_asset_image | Checks if object is instanceof Asset\Image |
pimcore_asset_text | Checks if object is instanceof Asset\Text |
pimcore_asset_unknown | Checks if object is instanceof Asset\Unknown |
pimcore_asset_video | Checks if object is instanceof Asset\Video |
pimcore_data_object | Checks if object is instanceof DataObject\Concrete |
pimcore_data_object_folder | Checks if object is instanceof DataObject\Folder |
pimcore_data_object_class(classname) | Checks if object is instanceof Pimcore\Model\DataObject{Classname} |
pimcore_data_object_gallery | Checks if object is instanceof DataObject\Data\ImageGallery |
pimcore_data_object_hotspot_image | Checks if object is instanceof DataObject\Data\Hotspotimage |
pimcore_document | Checks if object is instanceof Document |
pimcore_document_email | Checks if object is instanceof Document\Email |
pimcore_document_folder | Checks if object is instanceof Document\Folder |
pimcore_document_hardlink | Checks if object is instanceof Document\Hardlink |
pimcore_document_page | Checks if object is instanceof Document\Page |
pimcore_document_link | Checks if object is instanceof Document\Link |
pimcore_document_page_snippet | Checks if object is instanceof Document\PageSnippet |
pimcore_document_snippet | Checks if object is instanceof Document\Snippet |
The following tests are only available if the PimcoreWebToPrintBundle is enabled and installed:
| Test | Description |
|---|---|
pimcore_document_print | Checks if object is instanceof PrintAbstract |
pimcore_document_print_container | Checks if object is instanceof Printcontainer |
pimcore_document_print_page | Checks if object is instanceof Printpage |
The following tests are only available if the PimcoreNewsletterBundle is enabled and installed:
| Test | Description |
|---|---|
pimcore_document_newsletter | Checks if object is instanceof Newsletter |
You can also create your own custom Twig Extension to make certain functionalities available to your views.
Here you can find an example how to create
your own Twig Extension.
pimcorecache
This is an implementation of an in-template cache. You can use this to cache some parts directly in the template, independent of the other global definable caching functionality. This can be useful for templates which need a lot of calculation or require a huge amount of objects (like navigations, ...).
{% pimcorecache "cache_key" tags([...]) ttl(int) force(bool) %}
| Name | Type | Description |
|---|---|---|
cache_key | string | Key/name of cache item |
tags | string, string[] | One or multiple additional cache tags. The in_template cache tag is automatically added to all items. When no ttl is defined the output cache tag is additionally added. |
ttl | int | Time to life - lifetime in seconds. If you define no ttl the behavior is like the output cache, so if you make any change in Pimcore, the cache will be flushed. When specifying a lifetime this is independent from changes in the CMS. |
force | bool | Force caching, even when request is done within Pimcore Studio |
Examples
{% pimcorecache "test_cache_key" ttl(60) %}
<h1>This is some cached microtime</h1>
{{ 'now'|date('U') }}
{% endpimcorecache %}
{# example with all options #}
{% pimcorecache "test_cache_key" ttl(60) tags(['custom_tag']) force(true) %}
<h1>This is some cached microtime</h1>
{{ 'now'|date('U') }}
{% endpimcorecache %}
pimcore_cache (deprecated)
This is a deprecated alternative approach to the pimcorecache extension. Use pimcorecache instead.
pimcore_cache( name, lifetime, force)
| Name | Type | Description |
|---|---|---|
name | string | Name of cache item |
lifetime | int | Lifetime in seconds. If you define no lifetime the behavior is like the output cache, so if you make any change in Pimcore, the cache will be flushed. When specifying a lifetime this is independent from changes in the CMS. |
force | bool | Force caching, even when request is done within Pimcore Studio |
Example
{% set cache = pimcore_cache("test_cache_key", 60) %}
{% if not cache.start() %}
<h1>This is some cached microtime</h1>
{{ 'now'|date('U') }}
{% do cache.end() %}
{% endif %}
pimcore_device
This extension makes it easy to implement "Adaptive Design" in Pimcore.
Arguments
| Name | Type | Description |
|---|---|---|
default | string | optional Default if no device can be detected |
Example
{% set device = pimcore_device('desktop') %}
{% if device.isPhone() %}
This is my phone content
{% elseif device.isTablet() %}
This text is shown on a tablet
{% elseif device.isDesktop() %}
This is for default desktop Browser
{% endif %}
For details also see Adaptive Design.
pimcore_placeholder
See Placeholder Template Extension
pimcore_head_link
See HeadLink Template Extension
pimcore_head_meta
See HeadMeta Template Extension
pimcore_head_script
See HeadScript Template Extension
pimcore_head_style
See HeadStyle Template Extension
pimcore_head_title
See HeadTitle Template Extension
pimcore_inc
Use pimcore_inc() to include documents (eg. snippets) within views.
This is especially useful for footers, headers, navigations, sidebars, teasers, ...
pimcore_inc(document, params, cacheEnabled)
| Name | Type | Description |
|---|---|---|
document | PageSnippet | int | string | Document to include, can be either an ID, a path or even the Document object itself |
params | array | Is optional and should be an array with key value pairs. |
enabledCache | bool | Is true by default, set it to false to disable the cache. Hashing is done across source and parameters to ensure a consistent result. |
Example
{# include path #}
{{ pimcore_inc("/shared/boxes/buttons") }}
{# include ID #}
{{ pimcore_inc(256) }}
{# include document object #}
{% set doc = pimcore_document(477) %}
{{ pimcore_inc(doc, {param: 'value'}) }}
{# disable caching #}
{{ pimcore_inc(123, null, false) }}
When passing parameters to something included with pimcore_inc(), these parameters are not automatically passed to Twig. The parameters are passed as attributes to the included document, and should be passed to Twig via the document's controller action.
Example:
index.html.twig
{{ pimcore_inc('/some/other/document', { 'parameterToPass': parameterToPass }) }}
IndexController.php (whatever controller / method is designated for /some/other/document in the document tree)
public function otherDocumentAction(Request $request): array
{
return ['parameterToPass' => $request->query->get('parameterToPass')];
}
The controller passes the parameter to the template, where it becomes available as a Twig variable.
pimcore_inline_script
See InlineScript Template Extension
Navigation
pimcore_build_navpimcore_render_navpimcore_nav_renderer
Used to interact with navigations. See Navigation for details. Simplified example:
{% set navigation = pimcore_build_nav({
active: document,
root: navRootDocument
}) %}
{{ pimcore_render_nav(navigation) }}
{# you can also fetch the renderer instance and call custom render methods #}
{% set renderer = pimcore_nav_renderer('menu') %}
{{ renderer.render(navigation) }}
pimcore_url
An alternative to url() and path() with additional Pimcore-specific features.
{{ pimcore_url(params, name, reset, encode, relative) }}
All parameters are optional here:
| Name | Type | Description |
|---|---|---|
params | array | Route params. If object is passed in the params then link generator will be used to generate Url |
name | string | Route name |
reset | bool | Is false by default, set it to false to avoid merging parameters from request |
encode | bool | Is true by default, set it to false to disable encoding |
relative | bool | Is false by default, set it to true to generate a relative path based on the current request path |
Example
{% set object = pimcore_object(769) %}
{{ pimcore_url({'object': object}) }}
Loading Elements
The following functions load Pimcore elements by ID or path:
{# Load by ID #}
{% set myDocument = pimcore_document(1) %}
{% set myAsset = pimcore_asset(5) %}
{% set myObject = pimcore_object(123) %}
{% set myUser = pimcore_user(2) %}
{# Load by path #}
{% set myDocument = pimcore_document_by_path('/en/about') %}
{% set myAsset = pimcore_asset_by_path('/images/logo.png') %}
{% set myObject = pimcore_object_by_path('/products/my-product') %}
For documents, Pimcore also provides pimcore_document_wrap_hardlink() to handle hardlink contexts:
{% set wrappedDoc = pimcore_document_wrap_hardlink(document) %}
To get the current site:
{% set site = pimcore_site() %}
See PimcoreObjectExtension for implementation details.
Element Helper Functions
These functions retrieve specific data from Pimcore elements:
| Function | Description |
|---|---|
pimcore_object_classificationstore_group(id) | Get classification store group definition by ID. |
pimcore_object_classificationstore_get_field_definition_from_json(definition, type) | Returns the classification store field definition from the given definition JSON, enabling structured access to field properties. |
pimcore_object_brick_definition_key(key) | Get objectbrick definition by key. |
pimcore_element_tags(element, asNameList) | Returns a list of assigned element tags. Use true as the second parameter to receive only the tag names. |
Block Elements
As Twig does not provide a while control structure needed to iterate a
Block editable,
Pimcore provides the pimcoreblock / endpimcoreblock tags:
{% pimcoreblock "contentblock" %}
<h2>{{ pimcore_input("subline") }}</h2>
{{ pimcore_wysiwyg("content") }}
{% endpimcoreblock %}
Alternatively, use the pimcore_iterate_block function for programmatic iteration:
{% for i in pimcore_iterate_block(pimcore_block("myBlock")) %}
{{ pimcore_input("subline") }}
{% endfor %}