Skip to content

Latest commit

 

History

History
191 lines (89 loc) · 17.1 KB

File metadata and controls

191 lines (89 loc) · 17.1 KB

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
apply deprecated flush import use
autoescape do for include verbatim
block embed from macro with
cache extends if set
Functions
attribute cycle include random
block date max range
constant dump min source
country_timezones html_classes parent template_from_string
Filters
abs escape join number_format timezone_name
batch filter json_encode raw title
capitalize first keys reduce trim
column format language_name replace u
convert_encoding format_currency last reverse upper
country_name format_date length round url_encode
currency_name format_datetime locale_name slice
currency_symbol format_number lower slug
data_uri format_time map sort
date html_to_markdown markdown_to_html spaceless
date_modify inky_to_html merge split
default inline_css nl2br striptags
Tests
constant empty null
defined even odd
divisibleby iterable sameas
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
humanize trans
yaml_encode yaml_dump
serialize
Functions
absolute_url expression form_label form_widget relative_path
asset form form_parent is_granted render
asset_version form_end form_rest logout_path render_esi
controller form_errors form_row logout_url url
csrf_token form_help form_start path

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:

{% 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.

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