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>{{ data.menu_name }}</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);
header.twig
<ul>
{% for item in data %}
{{ include('partials/item-nav.twig') }}
{% endfor %}
</ul>
item-nav.twig
<li>{{ item.<other-property> }}</li>
Related
DefaultController.php
public function indexAction(Request $request) {
if ($request->get ( 'email' )) {
$email = $request->get ( 'email' );
$em = $this->getDoctrine ();
$mst_USERS = $em->getRepository ( 'AppBundle:mst_USERS' );
$mst_USERS->findAll ();
return $this->render ( 'default/main.html.twig', array (
'email' => $email,
'mst_USERS' => $mst_USERS
) );
}
main.html.twig
{% block styleSheet %}
<style>
</style>
{% endblock %}
{% block body %}
<div id="container">
THIS IS
{{email}}
<ul>
{% for mst_USERS in mst_USERS %}
<li>{{mst_USERS.email}}</li>
{% endfor %}
</ul>
</div>
{% endblock %}
{% block styleSheet %}
<style>
</style>
{% endblock %}
I tried to use AppBundle:mst_USERS entity for DefaultController but It doesn't work at all.
It was working on another controller, named mst_USERSController.php
Can't I using it on another controller or is there something wrong?
Try this at home
{% for user in mst_USERS %}
<li>{{user.email}}</li>
{% endfor %}
And in the controller add
return $this->render ( 'default/main.html.twig', array (
'email' => $email,
'mst_USERS' => $mst_USERS->findAll()
));
[Entity file][1]
Controller
thx for your help
In Controller
$images = array();
$em = $this->getDoctrine->getManager();
$probleme = $em->getRepository('PiCrowdRiseWebBundle:Probleme')->findAll();
foreach ($probleme as $key => $entity) {
$images[$key] = base64_encode(stream_get_contents($entity->getFoto()));
}
// ...
return $this->render('PiCrowdRiseWebBundle:Probleme:problemList.html.twig', array(
'problemes' => $probleme,
'images' => $images,
));
View:
{% for key, entity in problemes %}
{# ... #}
<img alt="Embedded Image" src="data:image/png;base64,{{ images[key] }}" />
{# ... #}
{% endfor %}
I dont have access to variables when try to include other file.
I try to add with keyword in include but dont work, all time i get message:
Variable "entity" does not exist in
ISLabBundlesBlogBundle:Post:last_post.html.twig at line 6
First i have indexAction() where i list all published blog post.
public function indexAction()
{
$posts = $this->getDoctrine()->getRepository('ISLabBundlesBlogBundle:Post')->getAllPublishedPosts();
return $this->render('ISLabBundlesBlogBundle:Page:index.html.twig', array(
'posts' => $posts
));
}
And have method where list only last posts
public function lastPostAction($count = 1)
{
$entity = $this->getDoctrine()->getRepository('ISLabBundlesBlogBundle:Post')->getLastPosts($count);
return $this->render('ISLabBundlesBlogBundle:Post:last_post.html.twig', array(
'entity' => $entity
));
}
Problem is in this file in block sidebar. I try to include other file where i fetch only 1 last post.
{% extends 'ISLabBundlesBlogBundle::layout.html.twig' %}
{% block body %}
{# Fetch all post#}
{% if posts %}
{% for post in posts %}
<article class="blog">
<div class="date"><time datetime="{{ post.created|date('c') }}">{{ post.created|date('l, F j, Y') }}</time></div>
<header><h2> {{ post.title }} </h2></header>
<p> {{ post.body }} </p>
</article>
{% endfor %}
{% endif %}
{% endblock %}
{% block sidebar %}
{% include 'ISLabBundlesBlogBundle:Post:last_post.html.twig'%}
{% endblock %}
And here is file what i try to include:
<h2>Last Posts</h2>
<div class="blog">
<ul>
{% for item in entity %}
<li>{{item.title}}</li>
{% endfor %}
</ul>
</div>
What i do wrong? And how to slove this?
You need to pass entity to your index template:
public function indexAction()
{
$posts = $this->getDoctrine()->getRepository('ISLabBundlesBlogBundle:Post')->getAllPublishedPosts();
$entity = $this->getDoctrine()->getRepository('ISLabBundlesBlogBundle:Post')->getLastPosts(1);
return $this->render('ISLabBundlesBlogBundle:Page:index.html.twig', array(
'posts' => $posts,
'entity' => $entity,
));
}
It's also possible (and probably better) to do this with an embedded controller:
{% block sidebar %}
{{ render(controller('ISLabBundlesBlogBundle:Post:lastPost', {
'count': 1
})) }}
{% endblock %}
http://symfony.com/doc/current/book/templating.html (search for Embedding Controllers)
How can I access a field in collection in twig
$builder
->add('name', 'text', array('required' => false))
->add('email', 'collection', array(
'type' => new UsersEmailAddressesType(),
'allow_add' => true
))
UserEmailAddressesType has two fields name and email, how can I access email field in twig ?
In the symfony cookbook there is an example on how to embed collections in forms. The solution there looks like this (adapted to your form example):
<ul>
{% for email in form.email %}
<li>{{ form_row(email.address) }}</li>
{% endfor %}
</ul>
Since you want to place the inputs next to each other you might want to check whether the loop.index is odd, even or divisibleby() for example like this:
{% for email in form.email %}
{% if loop.index is odd %}
<li class="float-left">
{% else %}
<li class="float-right">
{% endif %}
{{ form_row(email.address) }}</li>
{% endfor %}
It seems that Symfony2 Form component does not handle this common case. Below is what I want in my html
The code looks like :
->add('code', 'choice', array(
'choices' => array(
'Food' => array('pizza', 'burger', 'icecream'),
'Music' => array('poney', 'little', 'pocket'),
),
'multiple' => true,
'expanded' => true,
'required' => true
));
Which gives in reality the wrong output :
It's wierd because the case with expanded => false is correctly handled
How to handle that case please ?
Ok so here's the form_theme solution for this
{% block _user_code_widget %}
<div {{ block('widget_container_attributes') }}>
{% for name, choices in form.vars.choices %}
<ul>
<li class="checkbox_category">
<input type="checkbox" class="checkallsub" /> {{ name }}
</li>
{% for key,choice in choices %}
<li class="indent">
{{ form_widget(form[key]) }}
{{ form_label(form[key]) }}
</li>
{% endfor %}
</ul>
{% endfor %}
</div>
{% endblock %}
Of course the extra js layer is missing, but you get the idea.