2
votes

Want to create common header and footer templates that are included on several html pages in Symfony2. it gives me error:

Unable to find template "Bundle:Controller:header.html.twig" in base.html.twig at line 16.

Directory
src
/HelloBundle
/Controller
MainController.php
/Resources
/views
/Main
header.html.twig
index.html.twig

Code in my index.html.twig file:

<div class="container">
 <div id="header">
{% include "HelloBundle:Main:header.html.twig" %}
</div>
</div>

in my base.html.twig file:

    <html>
    <head>
        <meta charset="UTF-8" />
        <title></title>
        {% block stylesheets %}
    </head>
    <body>
16:         {% include 'Bundle:Controller:header.html.twig' %}
        {% block body %}

        {% endblock %}

          {% block javascripts %}            
           {% endblock %}
    </body>
</html>

and my header.html.twig

 <nav class="navbar navbar-default" role="navigation">
      <div class="container-fluid">

      <div class="navbar-header">
          <ul class="nav navbar-nav">
       <li class="active"><a href="{{ path('index') }}">Home</a></li>                          
       <li><a href="{{ path('english') }}"> LEARNING</a></li>

          </ul>

           </div>   <!-- collapse navbar-collapse -->
      </div>   <!-- container-fluid-->
    </nav>   <!--navbar navbar-default -->
1
What is your directory structure?Michael Sivolobov

1 Answers

5
votes

Assuming you have a views directory under Resources folder:

src/Acme/DemoBundle/Resources/views

Create a general layout template file (let's call it layout.html.twig), header.html.twig and footer.html.twig under views directory. And write a markup for layout.html.twig file:

<html>
    <head>
        <meta charset="UTF-8" />
        <title></title>
    </head>
    <body>
        <div id="header">
        {% include('@AcmeDemo/header.html.twig') %}
        </div>

        <div id="wrapper">
        {% block content %}{% endblock %}
        </div>


        <div id="footer">
        {% include('@AcmeDemo/footer.html.twig') %}
        </div>
    </body>
</html>

That's it. If you want to create another template (let's say product.view.html.twig) just extend that layout.html.twig:

product.view.html.twig

{% extends '@AcmeDemo/layout.html.twig' %}
{% block content %}{% endblock %}

Notice that I'am using namespaced syntax, I think it's more readable, and it is considered faster than usual syntax.