Version: Edit on GitHub

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:

  • render
  • render_esi
  • controller
  • asset
  • csrf_token
  • path
  • url
  • absolute_url
  • translator
  • trans
  • url
  • relative_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.

Extension Description
pimcore_cache() Simple in-template caching functionality
pimcore_device() Helps implementing adaptive designs
pimcoreglossary() Extension to control the glossary engine
pimcore_placeholder() Adding and embedding custom placeholders, e.g. for special header tags, etc.
pimcore_head_link() Embeding / managing referenced stylesheets (alternative to assets())
pimcore_head_meta() > elements in your HTML document
pimcore_head_script() > elements
pimcore_head_style() Managing inline styles (pendant 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 (pendant 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 also adds some Twig tests for evaluating boolean conditions e.g.

{# using 'instaceof' 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
pimcore_asset_audio
pimcore_asset_document
pimcore_asset_folder
pimcore_asset_image
pimcore_asset_text
pimcore_asset_unknown
pimcore_asset_video
pimcore_data_object
pimcore_data_object_folder
pimcore_data_object_class(classname)
pimcore_data_object_gallery
pimcore_data_object_hotspot_image
pimcore_document Checks if object is instanceof Document
pimcore_document_email
pimcore_document_folder
pimcore_document_hardlink
pimcore_document_newsletter
pimcore_document_page
pimcore_document_link
pimcore_document_page_snippet
pimcore_document_print
pimcore_document_print_container
pimcore_document_print_page
pimcore_document_snippet

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.

pimcore_cache

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, ...).

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 admin interface
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.

pimcoreglossary

The pimcoreglossary block replaces glossary terms. See Glossary for details.

{% pimcoreglossary %}
My content
{% endpimcoreglossary %}

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 object#}
{% set doc = pimcore_doc(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) {
    return ['parameterToPass' => $request->get('parameterToPass')];
}

more Convenient way

public function otherDocumentAction(Request $request) {
    return $this->render(":Default:someOtherDocument.html.twig", ['parameterToPass' => $request->get('parameterToPass')]);
}

someOtherDocument.html.twig (whatever Twig template is actually for /some/other/document in the document tree)

...
{{ parameterToPass }}
...

pimcore_inline_script

See InlineScript Template Extension

  • pimcore_build_nav
  • pimcore_render_nav
  • pimcore_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() which used Url.

{{ 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}) }}