Answer the question
In order to leave comments, you need to log in
How to use a template in Twig depending on the situation?
There are several Twig templates, and the page where you want to display them depending on the value of the variable. But for some reason Twig ignores the conditions and uses the last included template, in this example "template2.twig".
{% extends 'layout.twig' %}
{% if foo == bar %}
{% use 'template1.twig' %}
{% else %}
{% use 'template2.twig' %}
{% endif %}
{% block content %}{% endblock %}
Answer the question
In order to leave comments, you need to log in
It turned out to solve the problem with a simple investment.
There is a layout.twig file with the base template in it, then an index.twig file with the page template in it, and separate files for each of the microtemplates.
layout.twig :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
{% extends 'layout.twig' %}
{% block content %}
{% block info %}{% endblock %}
{% endblock %}
{% extends 'index.twig' %}
{% block info %}Hello World{% endblock %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question