S
S
startproger2019-11-09 16:05:26
symfony
startproger, 2019-11-09 16:05:26

How to pass different variables to different blocks in TWIG?

Let's say I have base.html.twig in which there are several blocks: head, breadcrumbs and the main content.
When I render a template (base or one that inherits base), I can pass all the variables I have to the template and they will all be visible in all blocks.
Is it possible to make it so that when rendering, determine which block which variables go to? So that some variables are visible only in one block and in no other.
Example, this would be base

<!DOCTYPE html>
<html>
  <head>
    <!-- Тут просто какой-то код -->
  </head>
  <body>
    <!-- Тут, допустим, хлебные крошки -->
    {% block breadcrumbs %}
      {% for breadcrumb in breadcrumbs %}
        <a href="{{ breadcrumb.link }}">{{ breadcrumb.title }}</a>
      {% endfor %}
    {% endblock %}
    
    <!-- Тут основной блок: -->
    {% block content %}{% endblock %}
  </body>
</html>

And this is a template for some content:
{% extends "base.twig" %}

{% block content %}
  <!-- Тут контент основного блока -->
{% endblock %}

I want to make sure that variables that are visible inside the breadcrumbs block are not visible inside the content block, and vice versa. If this is of course possible.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question