我希望开始使用 Twig,但是让 {% block %} 完全工作感到非常头疼 - 我觉得一定有一些非常明显的东西我错过了。
我的index.php加载器如下所示:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once( "Twig/Autoloader.inc.php" );
Twig_Autoloader::register();
$twig = new Twig_Environment( new Twig_Loader_Filesystem("templates"));
$vars = array (
"user" => array(
"name" => "Joe Bloggs"
),
"title" => "My website"
);
$tpl = $twig->loadTemplate("index.html");
echo $tpl->render($vars);
?>
index.html的简化版本/templates
如下所示:
<!doctype html>
<html>
<body>
Hello World!
{% block navigation %}Test{% endblock %}
</body>
</html>
中的navigation.html/templates
看起来像这样:
{% extends "index.html" %}
{% block navigation %}
<!-- Navigation -->
<nav>
Some navigation
</nav>
{% endblock %}
据我了解,这应该是块功能的基本工作示例。Twig 的其他方面对我来说似乎工作得很好,并且没有报告错误。确实,页面成功打印了“Test”。
我应该明确指向某个地方的navigation.html文件,还是 Twig 会自动加载文件/templates
夹中的所有文件?