使用嵌套包含Twig

时间:2017-05-06 07:26:10

标签: symfony twig templating twig-extension templating-engine

header.twig

<ul class="nav list-unstyled">
    {% for item in data %}
       {% include 'partials/item/header/item-nav.twig' %}
    {% endfor %}
</ul>

在我的item-nav.twig

<li><a href="{{ data.url }}">{{ data.menu_name }}</a></li>

header.twig包含在base.twig中。我在base.php文件中渲染了base.twig。我可以在哪里添加数据array来处理data.urldata.name?在base.php?或者我再次呈现文件php?谢谢。

base.php

include __DIR__ . '/../vendor/autoload.php';

$renderer = new \Resilient\TwigRenderer(__DIR__ . '/../vemale-beta-desktop/templates/', ['debug'    => true]);

$renderer->addExtension(new Twig_Extension_Debug());

$faker = Faker\Factory::create();

$data = [
    'assets_url' => 'http://devel.merdeka.com/ui/2017/april/Vemale/',
    'meta_title' => 'Vemale Beta',
    'meta_description' => 'Vemale Beta Desktop',
    'data' => ['menu_name' => 'PARENTING']
];

/*foreach ( range(0, 10) as $k ) {
    $data['data'][] = [ 'title' => $faker->sentence() ];
}*/

$response = $renderer->render(new \Zend\Diactoros\Response(),'base.twig', $data);

$emitter = new Zend\Diactoros\Response\SapiEmitter();

$emitter->emit($response);

1 个答案:

答案 0 :(得分:-1)

<强> header.twig

<ul>
    {% for item in data %}
        {{ include('partials/item-nav.twig') }}
    {% endfor %}
</ul>

<强>项-nav.twig

<li><a href="{{ item.<some-property> }}">{{ item.<other-property> }}</a></li>