1

I'd like to set a variable in my node.html.twig template:

{% set title = label %}
{{ content }}

The content consists of one field visible only, which has it's own template:

file-link.html.twig

How can I access the title variable of the node template in the file template? I am not sure if importing or extending is the right way since that might cause infinite recursion?

Edit

The file template looks like this (from the bootstrap theme):

{% spaceless %}
  {%
    set classes = [
      icon_only ? 'icon-only',
      not icon_only ? 'icon-' ~ icon_position|clean_class
    ]
  %}
  <span{{ attributes.addClass(classes) }}>
    {% if icon_only %}
      <span class="file-icon">{{ icon }}</span>
      <span class="sr-only">
        <span class="file-link">{{ link }}</span>
        <span class="file-size">{{ file_size }}</span>
      </span>
    {% else %}
      {% if icon_position == 'after' %}
        <span class="file-link">{{ link }}</span><span class="file-size">{{ file_size }}</span><span class="file-icon">{{ icon }}</span>
      {% else %}
        <span class="file-icon">{{ icon }}</span><span class="file-link">{{ link }}</span><span class="file-size">{{ file_size }}</span>
      {% endif %}
    {% endif %}
  </span>
{% endspaceless %}

Within that template I'd like to print the title of the respective node:

<span class="file-link">{{ link }}</span>
<span class="file-node-title">{{ title }}</span> {# << #}
<span class="file-size">{{ file_size }}</span>
0

EDIT

You could write a theme hook in your "mytheme.theme", modifiy the "variables" array and add title to the specific field AND then move the template file (in the original post) to your own theme template directory, make the changes using your new variable (called maybe title) which was provided from theme hook.


OLD:

First of all, "node.html.twig" is TOO general to claim that it's content in the future will consist only one field (file-link).

At least rename it to "node--my-content-type.html.twig". In the file-link.html.twig you won't able to reach the "title" variable (label) of the node.

You have the following work around: {{ content|without('field_file_link') }} {% include directory ~ '/templates/my-custom-twig.html.twig' with {'title': title} %}

Which means: 1. exclude your field from content. 2. create your own twig template 3. Pass any variable from the caller template you would like to pass.

Twig syntax / semantics is still changing, maybe my twig codes of lines can lead to error, but at least it will give you a hint / clue how to achieve this.

| improve this answer | |
  • In real it's not just node.html.twig, but for the sake of simplicity I called it like that. And I obviously rely on the file template so I cant just create a new blank template - the field must be rendered – Alex Jun 9 '16 at 8:36
0

What about hook_preprocess_HOOK with a relevant HOOK_ID (page, node, view ...) and declare your variables into it ?

In the youtheme.theme file

function MYTHEME_preprocess_page(&$variables) {
  $variables['foo'] = "bar";
}

In the twig one,

The content of Foo var is : {{ foo }}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.