Skip to content

Twig

The ems project heavily uses TWIG.

  • ElasticMS Admin uses twig for: custom dashboards, views, actions, postprocessing, ..
  • ElasticMS Web uses twig for creating webpages, redirect, pdf exports, file serving

Twig cheatsheet

Tags
applydeprecatedflushimportuse
autoescapedoforincludeverbatim
blockembedfrommacrowith
cacheextendsifset
Functions
attributecycleincluderandom
blockdatemaxrange
constantdumpminsource
country_timezoneshtml_classesparenttemplate_from_string
Filters
absescapejoinnumber_formattimezone_name
batchfilterjson_encoderawtitle
capitalizefirstkeysreducetrim
columnformatlanguage_namereplaceu
convert_encodingformat_currencylastreverseupper
country_nameformat_datelengthroundurl_encode
currency_nameformat_datetimelocale_nameslice
currency_symbolformat_numberlowerslug
data_uriformat_timemapsort
datehtml_to_markdownmarkdown_to_htmlspaceless
date_modifyinky_to_htmlmergesplit
defaultinline_cssnl2brstriptags
Tests
constantemptynull
definedevenodd
divisiblebyiterablesameas
Operators
in
is
Math (+, -, /, %, //, *, **)
Logic (and, or, not, (), b-and, b-xor, b-or)
Comparisons (==, !=, <, >, >=, <=, ===, starts with, ends with, matches)
Others (.., , ~, ., [], ?:, ??)

Symfony cheatsheet

Tags
trans
trans_default_domain
Filters
humanizetrans
yaml_encodeyaml_dump
serialize
Functions
absolute_urlexpressionform_labelform_widgetrelative_path
assetformform_parentis_grantedrender
asset_versionform_endform_restlogout_pathrender_esi
controllerform_errorsform_rowlogout_urlurl
csrf_tokenform_helpform_startpath

Common Bundle

The common's twig filters and functions are available in both ElasticMS Admin and in ElasticMS Web

Filter

ems_dom_crawler

This filter parses a string and returns a Symfony DomCrawler

Useful to extract content from a html string: {% set firstP = data.body_fr|ems_dom_crawler.filter('p').first.text %}

Another example for adding the class img-fluid to all img tags:

twig
{% set crawler = body|emsch_routing|ems_dom_crawler %}
{% for img in crawler.filter('img') %}
    {% if img.getAttribute('class') == '' %}
        {% do img.setAttribute('class', 'img-fluid') %}
    {% endif %}
{% endfor %}
{{ crawler.filter('body').html()|raw }}

Function

ems_template_exists

Returns true if the template exists. Also for only locally defined templates.

twig
{% set templateExists = ems_template_exists('@EMSCH/template/example.twig') %}