Functions and Filters Reference

This list provides an overview of provided functions and filters that are available inside twig templates.

Functions

blockExists

Usage Example

Checks if a block exists in the current layout.

{% if blockExists('page.main.title') %}
  {{ include('partials/page-title.html.twig', {pageTitle: getBlock('page.main.title').getPageHeading }) }}
{% endif %}

customerData

Usage Example

Fetches data from CustomerData like. Data like customer, wishlist or cart can be received there. Compare Magento Dev Docs.

{{ customerData('customer').fullname }}

getActiveFilters

Only useful on category pages. It receives the currently active filters.

Usage Example

{% if getActiveFilters()|length > 0 %}
  <div class="widget brands {%if filter.getData('swatchData')%}color{%endif%} mb-50">
      <!-- Widget Title 2 -->
      <p class="widget-title2 mb-30">{{'Now Shopping by'|trans}}</p>
      <div class="widget-desc">
        {% for activeFilter in getActiveFilters() %}
          <p>{{ activeFilter.filter.name }}: {{ activeFilter.label}} <a href="{{activeFilter.getRemoveUrl}}">[{{'Remove'|trans}}]</a></p>
        {% endfor %}
      </div>
  </div>
{% endif %}

getAsset

This is a shortcut for $this->template->getViewFileUrl("Wamoco_TwigTheme"), which gives the URL web assets are placed.

Usage Example

<link rel="stylesheet" href="{{ getAsset('/css/simple-grid.css') }}" async defer>

getBaseUrl

Gets the baseUrl for the current store view. This value needs also be present in JS to make some of the UI components work.

Usage Example

window.BASE_URL = "{{ getBaseUrl() }}";

getBlock

Receives a block which is defined in the current layout handles. All provided methods can be accessed.

Usage Example

{{ getBlock('product_list_toolbar').getFirstNum() }}

{{ getBlock('product.info').getProduct.price|price|raw }}

getCart

Receives cart data of current customer. Make sure to use this only on non-cachable pages.

Usage Example

{% for item in getCart().items %}
    {{ item.product.name }}
{% endfor %}

getCustomer

Gets information about the current customer. Make sure to use this only on non-cachable pages.

Usage Example

<p>{{ getCustomer().firstname }} {{ getCustomer().lastname }}</p>
<p>{{ getCustomer().email }}</p>

getFilters

Only useful on category pages. Gets a list of available filters including their swatch data.

Usage Example

{% for filter in getFilters() %}
  <p>{{filter.name}}</p>
  <ul>
    {% for filterItem in filter.items %}
      <li><a href="{{ filterItem.url }}">{{ filterItem.label|raw }}({ {filterItem.count }})</a></li>
    {% endfor %}
  </ul>
{% endfor %}

getFormKey

Gets the formKey. Usually you would rather render the form block like this: {{ renderBlock('formkey') }}.

Usage Example

<input type="hidden" name="form_key" value="{{ getFormKey }}">

getHelper

Receives any Helper by its class name.

Usage Example

<input name="name" id="name" title="{{'Name'|trans}}" value="{{ getHelper('Wamoco\\Theme\\View\\Helper\\Contact').getName }}" class="input-text form-control" type="text" data-validate="{required:true}"/>

getJs

Accessing Magento 2 Assets e.g. JavaScripts. This is equivalent for getViewFileUrl in Magento.

Usage Example

<script type="text/javascript" src="{{getJs('requirejs/require.js')}}"></script>

getProduct

Receives a product object by SKU or ID. If no argument is provided, the current product is returned, e.g. on product detail pages.

Usage Example

{{ getProduct('24-MB01') }}

getRequest

Gets the current request object.

Usage Example

{{ getRequestParam().getParam('token') }}

getRequestParam

Gets a request parameter by its name.

Usage Example

<input type="search" name="q" id="headerSearch" placeholder="{{'Search entire store here...'|trans}}" value="{{ getRequestParam('q')|escape }}">

getUrl

Uses the UrlBuilder of Magento to generate URLs.

Usage Example

<form method="POST" action="{{ getUrl('contact/index/post') }}">

getViewFileUrl

Accessing Magento 2 Assets e.g. JavaScripts. This is equivalent for getViewFileUrl in Magento.

Usage Example

<script type="text/javascript" src="{{getViewFileUrl('requirejs/require.js')}}"></script>

renderBlock

Render any block in the current layout by its name.

Usage Example

{{ renderBlock('customer.customer.data') }}

renderCmsBlock

Fetches a CMS block by its name from backend and renders it.

Usage Example

{{ renderCmsBlock('home-page-block') }}

renderElement

Renderes a layout element within Magento and returns the html. This is useful to render entire parts and place them somewhere.

Usage Example

{{ renderElement('sidebar.main') }}

Include a partial

Actually this is a built-in twig feature, but helpful for reference.

Usage Example

{% include 'pages/cms/shop/product.html.twig' with {'product': getProduct('24-MB01')} %}

Filters

formatDate

Usage Example

Formats a date according to the locale settings.

<td>{{order.getCreatedAt|formatDate}}</td>

escapeHtmlAttr

Usage Example

escapeUrl

Usage Example

price

Formats a price according to the currency settings of the current store view.

Usage Example

{{ getBlock('product.info').getProduct.price|price|raw }}

trans

Uses the built-in translation function to translate strings.

Usage Example

{{ 'Country'|trans }}