Reputation: 6461
I return data with my Controller.php to my twig template:
return $this->render('list.html.twig', ['page' => $page]);
On my list.html.twig I can output my variable:
{{page.name}}
But I extend my base.html twig on my list.html.twig:
{% extends 'base.html.twig' %}
So when I want to output my variable on my base.html.twig:
{{page.name}}
Then I get the error message:
Uncaught PHP Exception Twig_Error_Runtime: "Variable "page" does not exist." at /Users/work/project/templates/base.html.twig line 70
Upvotes: 2
Views: 3917
Reputation: 876
On your base.html.twig
add a block like
{% block page %}{% endblock page %}
and on list.html.twig
add {% block page %}{{page.name}}{% endblock page %}
So you could have a file like base.html.twig
<html>
<body>
<h1>{% block page %}{% endblock page %}
<p>{% block content%}{% endblock content %}</p>
</body>
</html>
and a list.html.twig
like
{% extends 'base.html.twig' %}
{% block page %}{{ page.name }}{% endblock page %}
{% block content %}errything else for page here{% endblock content %}
and it'd be valid and would overwrite the page block for base.html.twig through inheritance.
Added a twigfiddle example https://twigfiddle.com/d1hmo9
Upvotes: 3