2016-09-06 59 views
0

我觉得这是非常愚蠢的问题,但解决不了。枝条延伸不起作用

初始化

$loader = new Twig_Loader_Filesystem('template'); 
$twig = new Twig_Environment($loader, array(
    'cache' => 'c_cache', 
    'debug' => true, 
    'auto_reload' => true, 
)); 

渲染

echo $twig->render('layout.twig', array(
'data' => array(
    'title' => 'Page title!', 
))); 

layout.twig

通过安装作曲家嫩枝版本1.24.2

尝试基本功能,从扩展模板开始

... 
<body> 

{% block header %}{% endblock %} 

</body> 
... 

_header.twig

{% extends 'layout.twig' %} 

{% block header %} 
    Hello? 
{% endblock %} 

更新

我误解延长的概念,我应该使用包含在这种情况下。

+0

它是如何工作的?你在调用'$ twig-> render('layout.twig',...)',它只呈现'layout.twig'。也许你想调用'$ twig-> render('_ header.twig',...)'? – martin

+1

我想调用layout.twig并将它包含到partials _header.twig,_body.twig等等...... – RomkaLTU

+1

这不是'extends'的意思。你可能需要'include'标签,请参阅http://twig.sensiolabs.org/doc/tags/include.html – martin

回答

0

你的渲染应该来自_header.twig而不是来自layout.twig。如果渲染布局,块头是空的。

试着从_header.twig渲染,你会得到预期的结果。