我有2个TWIG渲染块在基础和标准模板上工作。这两个块在页面上呈现,所有在{% extends "base.php" %}
之前和{% block body %}{% endblock %}
之后的base.htm不会呈现,我明白了为什么我使用了renderblock
而不是render
,它应该呈现整个模板。 1我如何获得整个模板来渲染,并且除非我使用for循环,否则{% block head %}
不会渲染,我相信有更好的方法来做这件事。编辑1:添加$data2
。TWIG:使用扩展和渲染块渲染多个块
API
$template = $twig->loadTemplate('event.htm');
echo $template->renderBlock('head', (array('heads' => $data2)));
echo $template->renderBlock('content', (array('events' => $data)));
base.htm
<html>
<head>
{% block head %}
{% for head in heads %}
<title>{{ head.title }}</title>
<meta charset="UTF-8">
</head>
<body>
<h1>{{ head.title2 }}</h1>
{% endfor %}
{% endblock %}
{% block body %}{% endblock %}
</body>
</html>
event.htm
{% extends "base.php" %}
{% block content %}
{% for event in events %}
{{event.uri}}
{{event.desc}}
{% else %}
no events!
{% endfor %}
{% endblock %}
$ DATA 2
Array ([0] => Array ([id] => 1 [uri] => /event1/1 [title] => some title))
1/Works。我认为这是行不通的,因为块名称与数组不同,必须使用“renderblock”。 2 /作品,我传递一个数组(第一个TWIG模板,认为数组是预期的)。 '$ data2' ='Array([0] => Array([id] => 1 [uri] =>/event1/1 [title] =>一些标题)' – Jus