Using Nested Include Twig

泪湿孤枕 提交于 2019-12-08 13:11:16

问题


in my header.twig :

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

in my item-nav.twig

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

header.twig is included in base.twig. I rendered base.twig in base.php file. Where I can add data arrayto process data.url and data.name? in base.php ? or i rendered file php again ? Thank you.

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:


header.twig

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

item-nav.twig

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


来源:https://stackoverflow.com/questions/43817886/using-nested-include-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!