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 |
| 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 |
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') %}