想要创建Symfony2的多个html页面中包含的通用页眉和页脚模板。它给我错误:
在第16行的base.html.twig中找不到模板“ Bundle:Controller:header.html.twig”。
目录
src
/ HelloBundle
/ Controller
MainController.php
/ Resources
/ views
/ Main
header.html.twig
index.html.twig
我的index.html.twig文件中的代码:
<div class="container">
<div id="header">
{% include "HelloBundle:Main:header.html.twig" %}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在我的base.html.twig文件中:
<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>
Run Code Online (Sandbox Code Playgroud)
和我的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 -->
Run Code Online (Sandbox Code Playgroud)
假设您views在Resources文件夹下有一个目录:
src/Acme/DemoBundle/Resources/views
Run Code Online (Sandbox Code Playgroud)
创建总体布局模板文件(让我们称之为layout.html.twig),header.html.twig并footer.html.twig在views目录中。并为layout.html.twig文件写一个标记:
<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>
Run Code Online (Sandbox Code Playgroud)
而已。如果要创建另一个模板(比如说product.view.html.twig),只需扩展它layout.html.twig:
product.view.html.twig
{% extends '@AcmeDemo/layout.html.twig' %}
{% block content %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)
请注意,我使用的是命名空间语法,我认为它更具可读性,并且被认为比常规语法更快。