Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ojchris/223d4a3165bd979e1f33a2c09dcee599 to your computer and use it in GitHub Desktop.

Select an option

Save ojchris/223d4a3165bd979e1f33a2c09dcee599 to your computer and use it in GitHub Desktop.

Revisions

  1. @raphaellarrinaga raphaellarrinaga revised this gist May 13, 2020. No changes.
  2. @raphaellarrinaga raphaellarrinaga revised this gist Apr 29, 2020. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -190,4 +190,19 @@ function hops_theme_suggestions_block_alter(array &$suggestions, array $variable
    {{ node_2_teaser }}
    <a href="{{ path('entity.node.canonical', {'node': content.field_node_reference.1['#node'].id}) }}">{{ 'Learn more'|t }}</a>
    {% endblock %}
    ```

    ### Render referenced entity field as text

    Example here for a referenced paragraph but could work with a node with `tab['#node']`.

    ```
    {% for key, tab in content.field_reference_tab if key|first != '#' %}
    {% set body = {
    '#type': 'processed_text',
    '#text': tab['#paragraph'].get('field_body').getValue.0.value,
    '#format': tab['#paragraph'].get('field_body').getValue.0.format,
    } %}
    {{ body }}
    {% endfor %}
    ```
  3. @raphaellarrinaga raphaellarrinaga revised this gist Jan 10, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    ### Getting Drupal 8 field values in Twig

    Image path: `{{ file_url(content.field_name['#items'].entity.uri.value }}`
    Image path: `{{ file_url(content.field_name['#items'].entity.uri.value) }}`

    Image title text: `{{ node.field_name.title }}`

  4. @raphaellarrinaga raphaellarrinaga revised this gist Dec 28, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,12 @@

    ### Getting Drupal 8 field values in Twig

    Image path: `{{ file_url(content.field_name['#items'].entity.uri.value }}`

    Image title text: `{{ node.field_name.title }}`

    Entity Reference path: `{{ content.field_tags[0]['#url'] }}`

    @see https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd

    ### Check if field is filled
  5. @raphaellarrinaga raphaellarrinaga revised this gist Dec 28, 2018. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,6 @@

    ### Getting Drupal 8 field values in Twig

    Image `{{ file_url(content.field_name['#items'].entity.uri.value }}`

    Entity reference path `{{ content.field_tags[0]['#url'] }}`

    Plain text email: `{{ node.field_name.0.value }}`

    @see https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd

    ### Check if field is filled
    @@ -108,7 +102,6 @@ That synthax avoid errors if `classes` is initialy empty.
    ```
    {% set classes = { 'class' : 'teaser' }|merge({ (classes) : classes }) %}
    <article class="{{ classes|join(' ') }}" role="article">
    </article>
    ```

    ### Override custom layout templates (eg for a specific node)
  6. @raphaellarrinaga raphaellarrinaga revised this gist Dec 28, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,12 @@

    ### Getting Drupal 8 field values in Twig

    Image `{{ file_url(content.field_name['#items'].entity.uri.value }}`

    Entity reference path `{{ content.field_tags[0]['#url'] }}`

    Plain text email: `{{ node.field_name.0.value }}`

    @see https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd

    ### Check if field is filled
  7. @raphaellarrinaga raphaellarrinaga revised this gist Dec 28, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -79,7 +79,6 @@ And https://www.drupal.org/project/twig_extender
    {% for key, child in parent if key|first != '#' %}
    {{ child }}
    {% endfor %}
    ```

    @see https://drupal.stackexchange.com/questions/174742/loop-through-child-elements-in-twig-like-elementchildren/174743
    @@ -104,7 +103,6 @@ That synthax avoid errors if `classes` is initialy empty.
    {% set classes = { 'class' : 'teaser' }|merge({ (classes) : classes }) %}
    <article class="{{ classes|join(' ') }}" role="article">
    </article>
    ```

    ### Override custom layout templates (eg for a specific node)
  8. @raphaellarrinaga raphaellarrinaga revised this gist Dec 28, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -28,11 +28,11 @@ Those solutions are not perfect.

    Use Twig date filter + a defined Drupal date format

    {{ node.field_blog_date.value|date('U')|format_date('short_mdyyyy') }}
    `{{ node.field_blog_date.value|date('U')|format_date('short_mdyyyy') }}`

    Use Twig date filter

    {{ node.field_blog_date.value|date('n/j/Y') }}
    `{{ node.field_blog_date.value|date('n/j/Y') }}`

    ### Set variables in a translated twig variable

  9. @raphaellarrinaga raphaellarrinaga revised this gist Dec 26, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    ## Drupal 8 Twig cheatsheet

    ### Getting Drupal 8 field values in Twig

    @see https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd

    ### Check if field is filled

    ```
  10. @raphaellarrinaga raphaellarrinaga revised this gist Dec 17, 2018. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -170,4 +170,17 @@ function hops_theme_suggestions_block_alter(array &$suggestions, array $variable
    $suggestions[] = 'block__' . $bundle . '__' . $view_mode;
    }
    }
    ```

    ### Display a block entity reference field

    ```
    {% block content %}
    {% set node_1_teaser = content.field_node_reference.0|merge({'#view_mode': 'compact'}) %}
    {% set node_2_teaser = content.field_node_reference.1|merge({'#view_mode': 'compact'}) %}
    {{ node_1_teaser }}
    <a href="{{ path('entity.node.canonical', {'node': content.field_node_reference.0['#node'].id}) }}">{{ 'Learn more'|t }}</a>
    {{ node_2_teaser }}
    <a href="{{ path('entity.node.canonical', {'node': content.field_node_reference.1['#node'].id}) }}">{{ 'Learn more'|t }}</a>
    {% endblock %}
    ```
  11. @raphaellarrinaga raphaellarrinaga revised this gist Dec 11, 2018. 1 changed file with 0 additions and 27 deletions.
    27 changes: 0 additions & 27 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -170,31 +170,4 @@ function hops_theme_suggestions_block_alter(array &$suggestions, array $variable
    $suggestions[] = 'block__' . $bundle . '__' . $view_mode;
    }
    }
    ```

    ### Block class for custom block bundles.

    @see https://www.drupal.org/node/2724333

    ```
    /**
    * Implements hook_preprocess_block().
    */
    function hops_preprocess_block(&$variables) {
    // Add class for a specific block id.
    $block_id = $variables['elements']['#id'];
    switch ($block_id) {
    case '':
    $variables['attributes']['class'][] = '';
    break;
    default:
    break;
    }
    if (isset($variables['elements']['content']['#block_content'])) {
    $variables['attributes']['class'][] = 'block--'. $variables['elements']['content']['#block_content']->bundle();
    }
    }
    ```
  12. @raphaellarrinaga raphaellarrinaga revised this gist Sep 12, 2018. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,16 @@ Those solutions are not perfect.

    `{% set date = node.createdtime|format_date('long') %}`

    @see http://agileadam.com/2017/08/drupal-8-formatting-a-date-field-in-twig/

    Use Twig date filter + a defined Drupal date format

    {{ node.field_blog_date.value|date('U')|format_date('short_mdyyyy') }}

    Use Twig date filter

    {{ node.field_blog_date.value|date('n/j/Y') }}

    ### Set variables in a translated twig variable

    `<p class="submitted">{{ "Submitted by !author on @date"|t({ '!author': author, '@date': date }) }}</p>`
  13. @raphaellarrinaga raphaellarrinaga revised this gist Jun 28, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@

    ### Check for some content existence

    Those solutions are not perfect.
    @see https://www.drupal.org/project/drupal/issues/953034?page=1

    - e.g. for a region or a field that contains text `content.field_fee|render|striptags|trim`
    - e.g. for a field image using field value module `content.header.field_image|field_value`

  14. @raphaellarrinaga raphaellarrinaga revised this gist Jun 28, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,8 @@

    ### Check for some content existence

    `content.field_fee|render|striptags|trim`
    - e.g. for a region or a field that contains text `content.field_fee|render|striptags|trim`
    - e.g. for a field image using field value module `content.header.field_image|field_value`

    ### Custom date format

  15. @raphaellarrinaga raphaellarrinaga revised this gist Jun 14, 2018. 1 changed file with 28 additions and 11 deletions.
    39 changes: 28 additions & 11 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -104,16 +104,6 @@ et_twocols_hero:
    template: templates/et-twocols-hero
    ```

    ### Block class for custom block bundles.

    @see https://www.drupal.org/node/2724333

    ```
    if (isset($variables['elements']['content']['#block_content'])) {
    $variables['attributes']['class'][] = 'block--'. $variables['elements']['content']['#block_content']->bundle();
    }
    ```

    ### Provide splited views More link and title vars

    ```
    @@ -134,7 +124,7 @@ function hook_preprocess_views_view(&$variables){
    }
    ```

    ### Provide block classes
    ### Provide block classes from id

    - plugin_id : unique identifier provided for all blocks (duplicated one, displayed by context module)
    - elements['#id'] : unique identifier depending on block id defined via block layout UI, NULL is returned by context module
    @@ -166,4 +156,31 @@ function hops_theme_suggestions_block_alter(array &$suggestions, array $variable
    $suggestions[] = 'block__' . $bundle . '__' . $view_mode;
    }
    }
    ```

    ### Block class for custom block bundles.

    @see https://www.drupal.org/node/2724333

    ```
    /**
    * Implements hook_preprocess_block().
    */
    function hops_preprocess_block(&$variables) {
    // Add class for a specific block id.
    $block_id = $variables['elements']['#id'];
    switch ($block_id) {
    case '':
    $variables['attributes']['class'][] = '';
    break;
    default:
    break;
    }
    if (isset($variables['elements']['content']['#block_content'])) {
    $variables['attributes']['class'][] = 'block--'. $variables['elements']['content']['#block_content']->bundle();
    }
    }
    ```
  16. @raphaellarrinaga raphaellarrinaga revised this gist Jun 14, 2018. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -146,4 +146,24 @@ function hook_preprocess_views_view(&$variables){
    'block--' ~ (elements['#id'] ? elements['#id']|clean_class),
    ]
    %}
    ```

    ### Provide Custom block bundle templates / or classes (block types from custom block library)

    ```
    use \Drupal\block_content\BlockContentInterface;
    /**
    * Implements hook_theme_suggestions_block_alter().
    */
    function hops_theme_suggestions_block_alter(array &$suggestions, array $variables) {
    $content = $variables['elements']['content'];
    if (isset($content['#block_content']) and $content['#block_content'] instanceof BlockContentInterface) {
    $bundle = $content['#block_content']->bundle();
    $view_mode = $content['#view_mode'];
    $suggestions[] = 'block__' . $bundle;
    $suggestions[] = 'block__' . $view_mode;
    $suggestions[] = 'block__' . $bundle . '__' . $view_mode;
    }
    }
    ```
  17. @raphaellarrinaga raphaellarrinaga revised this gist Jun 14, 2018. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -132,4 +132,18 @@ function hook_preprocess_views_view(&$variables){
    '#markup' => $view->getTitle(),
    ];
    }
    ```

    ### Provide block classes

    - plugin_id : unique identifier provided for all blocks (duplicated one, displayed by context module)
    - elements['#id'] : unique identifier depending on block id defined via block layout UI, NULL is returned by context module

    ```
    {%
    set classes = [
    'block-' ~ plugin_id|clean_class,
    'block--' ~ (elements['#id'] ? elements['#id']|clean_class),
    ]
    %}
    ```
  18. @raphaellarrinaga raphaellarrinaga revised this gist Jun 14, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -121,7 +121,7 @@ if (isset($variables['elements']['content']['#block_content'])) {
    * Implements template_preprocess_views_view()
    * @param array $variables
    */
    function etuc_theme_preprocess_views_view(&$variables){
    function hook_preprocess_views_view(&$variables){
    $view = $variables['view'];
    $variables['more_url'] = $variables['more']['#url'];
  19. @raphaellarrinaga raphaellarrinaga revised this gist Jun 14, 2018. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -113,3 +113,23 @@ if (isset($variables['elements']['content']['#block_content'])) {
    $variables['attributes']['class'][] = 'block--'. $variables['elements']['content']['#block_content']->bundle();
    }
    ```

    ### Provide splited views More link and title vars

    ```
    /**
    * Implements template_preprocess_views_view()
    * @param array $variables
    */
    function etuc_theme_preprocess_views_view(&$variables){
    $view = $variables['view'];
    $variables['more_url'] = $variables['more']['#url'];
    $variables['more_text'] = $variables['more']["#title"];
    // @todo see usage of `title`, maybe combined with twig if on default template
    $variables['custom_title'] = [
    '#markup' => $view->getTitle(),
    ];
    }
    ```
  20. @raphaellarrinaga raphaellarrinaga revised this gist Jun 14, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -104,11 +104,11 @@ et_twocols_hero:
    template: templates/et-twocols-hero
    ```

    ###
    ### Block class for custom block bundles.

    @see https://www.drupal.org/node/2724333

    ```
    // Block class for custom block bundles.
    // @see https://www.drupal.org/node/2724333
    if (isset($variables['elements']['content']['#block_content'])) {
    $variables['attributes']['class'][] = 'block--'. $variables['elements']['content']['#block_content']->bundle();
    }
  21. @raphaellarrinaga raphaellarrinaga revised this gist Jun 14, 2018. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -103,3 +103,13 @@ et_twocols_hero:
    et_twocols_hero:
    template: templates/et-twocols-hero
    ```

    ###

    ```
    // Block class for custom block bundles.
    // @see https://www.drupal.org/node/2724333
    if (isset($variables['elements']['content']['#block_content'])) {
    $variables['attributes']['class'][] = 'block--'. $variables['elements']['content']['#block_content']->bundle();
    }
    ```
  22. @raphaellarrinaga raphaellarrinaga revised this gist May 31, 2018. 1 changed file with 0 additions and 20 deletions.
    20 changes: 0 additions & 20 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -103,23 +103,3 @@ et_twocols_hero:
    et_twocols_hero:
    template: templates/et-twocols-hero
    ```

    ### Set crappy custom class for layout

    ```
    {% set object = _context['content']['#node'] %}
    {%
    set classes = [
    'layout',
    ]
    %}
    {% if object %}
    {%
    set object_classes = [
    "layout--" ~ object.bundle ~ "-" ~ object.id,
    ]
    %}
    {% else %}
    {% set object_classes = [] %}
    {% endif %}
    ```
  23. @raphaellarrinaga raphaellarrinaga revised this gist May 31, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -111,8 +111,6 @@ et_twocols_hero:
    {%
    set classes = [
    'layout',
    'layout--twocols-hero',
    content.field_image|render|striptags('<img>')|trim|raw ? 'layout--has-aside-image',
    ]
    %}
    {% if object %}
  24. @raphaellarrinaga raphaellarrinaga revised this gist May 31, 2018. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -102,4 +102,26 @@ et_twocols_hero:
    ```
    et_twocols_hero:
    template: templates/et-twocols-hero
    ```

    ### Set crappy custom class for layout

    ```
    {% set object = _context['content']['#node'] %}
    {%
    set classes = [
    'layout',
    'layout--twocols-hero',
    content.field_image|render|striptags('<img>')|trim|raw ? 'layout--has-aside-image',
    ]
    %}
    {% if object %}
    {%
    set object_classes = [
    "layout--" ~ object.bundle ~ "-" ~ object.id,
    ]
    %}
    {% else %}
    {% set object_classes = [] %}
    {% endif %}
    ```
  25. @raphaellarrinaga raphaellarrinaga revised this gist May 31, 2018. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -87,4 +87,19 @@ That synthax avoid errors if `classes` is initialy empty.
    <article class="{{ classes|join(' ') }}" role="article">
    </article>
    ```

    ### Override custom layout templates (eg for a specific node)

    1. Be carreful to set

    ```
    et_twocols_hero:
    class: '\Drupal\ds\Plugin\DsLayout'
    ```

    2. Also name the template as the machine name otherwise suggestions provided by DS will not follow the same convention bu the one from machine name then Entity, bundle, view mode
    ```
    et_twocols_hero:
    template: templates/et-twocols-hero
    ```
  26. @raphaellarrinaga raphaellarrinaga revised this gist May 25, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,12 @@ or
    <p>{% trans %}Return to the <a href="{{ front_uri }}">homepage</a> or <a href="{{ search_uri }}">search articles</a>.{% endtrans %}</p>
    <p>{{ 'Return to the <a href="@front">homepage</a> or <a href="@search">search articles</a>.'|t({'@front': front_uri, '@search': search_uri}) }}</p>
    ```
    or
    ```
    {% set url_1 = path('entity.node.canonical', {'node': 1050}) %}
    {% set url_2 = path('entity.node.canonical', {'node': 2010}) %}
    {% trans %}I will have <a href="{{ url_1 }}">one link</a> and <a href="{{ url_2 }}">a second link.</a>{% endtrans %}
    ```
    @see - more on twig translation https://getlevelten.com/blog/mark-carver/drupal-8-twig-templates-and-translations

    ### Create a link
  27. @raphaellarrinaga raphaellarrinaga revised this gist May 7, 2018. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,10 @@
    {% endif %}
    ```

    ### Check for some content existence

    `content.field_fee|render|striptags|trim`

    ### Custom date format

    `{% set date = node.createdtime|format_date('long') %}`
    @@ -77,8 +81,4 @@ That synthax avoid errors if `classes` is initialy empty.
    <article class="{{ classes|join(' ') }}" role="article">
    </article>
    ```

    ### Check for some content existence

    `content.field_fee|render|striptags|trim`
    ```
  28. @raphaellarrinaga raphaellarrinaga revised this gist Apr 13, 2018. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,6 @@

    `<p class="submitted">{{ "Submitted by !author on @date"|t({ '!author': author, '@date': date }) }}</p>`

    @see - more on twig translation https://getlevelten.com/blog/mark-carver/drupal-8-twig-templates-and-translations

    or

    ```
    @@ -26,6 +24,15 @@ or
    <p>{% trans %}Return to the <a href="{{ front_uri }}">homepage</a> or <a href="{{ search_uri }}">search articles</a>.{% endtrans %}</p>
    <p>{{ 'Return to the <a href="@front">homepage</a> or <a href="@search">search articles</a>.'|t({'@front': front_uri, '@search': search_uri}) }}</p>
    ```
    @see - more on twig translation https://getlevelten.com/blog/mark-carver/drupal-8-twig-templates-and-translations

    ### Create a link

    ```
    <a href="{{ path('contact.site_page') }}">{{ 'Contact'|t }}</a>
    <a href="{{ url('contact.site_page') }}">{{ 'Contact'|t }}</a>
    {{ link('Contact'|t, 'base:contact', { 'class':[] }) }}
    ```
    @see - https://drupal.stackexchange.com/a/208107/30331

    ### Render a field to use it eg, in a translated string
  29. @raphaellarrinaga raphaellarrinaga revised this gist Apr 13, 2018. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -16,16 +16,17 @@

    `<p class="submitted">{{ "Submitted by !author on @date"|t({ '!author': author, '@date': date }) }}</p>`

    @see - more on twig translation https://getlevelten.com/blog/mark-carver/drupal-8-twig-templates-and-translations

    or

    ```
    {% set front_uri = path('<front>') %}
    {% set search_uri = "search/content" %}
    {% set search_uri = path('view.solr_search_content.page_1') %}
    <p>{% trans %}Return to the <a href="{{ front_uri }}">homepage</a> or <a href="{{ search_uri }}">search articles</a>.{% endtrans %}</p>
    <p>{{ 'Return to the <a href="@front">homepage</a> or <a href="@search">search articles</a>.'|t({'@front': front_uri, '@search': search_uri}) }}</p>
    ```

    @see - more on twig translation https://getlevelten.com/blog/mark-carver/drupal-8-twig-templates-and-translations
    @see - https://drupal.stackexchange.com/a/208107/30331

    ### Render a field to use it eg, in a translated string

  30. @raphaellarrinaga raphaellarrinaga revised this gist Apr 13, 2018. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions drupal_8_twig_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,15 @@

    `<p class="submitted">{{ "Submitted by !author on @date"|t({ '!author': author, '@date': date }) }}</p>`

    or

    ```
    {% set front_uri = path('<front>') %}
    {% set search_uri = "search/content" %}
    <p>{% trans %}Return to the <a href="{{ front_uri }}">homepage</a> or <a href="{{ search_uri }}">search articles</a>.{% endtrans %}</p>
    <p>{{ 'Return to the <a href="@front">homepage</a> or <a href="@search">search articles</a>.'|t({'@front': front_uri, '@search': search_uri}) }}</p>
    ```

    @see - more on twig translation https://getlevelten.com/blog/mark-carver/drupal-8-twig-templates-and-translations

    ### Render a field to use it eg, in a translated string