问题
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 array
to 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