2016-09-06 57 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渲染,你會得到預期的結果。