I have route that is using cUrl.
The cUrl returns Json data.
So, I did $data = json_decode($results);
It returns:
(
.....
[dovecot] => stdClass Object
(
[info_str] => Running
[name] => Dovecot
[version] => 2.3.4
)
[exim] => stdClass Object
(
[info_str] => Running
[name] => Exim
[version] => 4.91
)
.......
}
So when I do return $data; on my route to twig and do:
{% for info in data %}
{{ info }}
{% endfor %}
It doesn't show anything.
My point point is to print only the [name], so I also tried:
{% for info in data %}
{{ info.name }}
{% endfor %}
And it's not working.
Related
[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 built a form in symfony with this code:
foreach ( $options as $field => $title ) {
$form->add( $field, 'text', array('label' => $title, 'mapped' => false ) );
}
And I can't find a way to render all the rows of this form in twig.
Is this possible?
Ok. I've found how to do this.
In Twig:
{% for child in form.children|keys %}
{{ form_row(attribute(form.children,child)) }}
{% endfor %}
Are you looking for something like this : ?
{{ form(form) }}
or
{{ form_widget(form) }}
http://symfony.com/doc/current/cookbook/form/form_customization.html
Im new to Symfony / Twig and am having problems passing object values to my twig templates.
Here is some of my controller code that shows the content of the object:
$prevArticles = $section->getArticles();
print_r($prevArticles);
die()
Displays:
Array
(
[0] => Imagine\NewsletterBundle\Entity\Article Object
(
[id:protected] =>
[title:protected] =>
[headline:protected] =>
[link:protected] =>
[image:protected] =>
[excerpt:protected] =>
[check:protected] =>
[attachment:protected] =>
[field1:protected] =>
[field2:protected] =>
[field3:protected] =>
[magazines:protected] =>
[top_logo_advert:protected] => /uploaded_images/cece0b1859ea2b1af95f1f274620ba77.jpg
[top_logo_alt:protected] => Picture of blomange
[top_logo_link:protected] => www.google.com
)
)
So then I pass my object to my twig template like so:
return $this->render('ImagineNewsletterBundle:Section:'.$builder->getTemplate(), array('prevArticles' => $prevArticles));
Then in my twig template I want to display the value of 'top_logo_advert' but its not working:
{% for article in prevArticles %}
{{ article.top_logo_advert }}
{% endfor %}
I get the error message:
Method "top_logo_advert" for object "Imagine\NewsletterBundle\Entity\Article" does not exist in ImagineNewsletterBundle:Section:build_advert.html.twig at line 62
You must access it via :
{{ article.topLogoAdvert }} or {{ article.getTopLogoAdvert() }}
Both solutions works. Next time, just reminder that properties like 'my_property_1' is converted into myProperty1 in the twig engine.
I have two joined tables. With print_r I am getting something like this
Array
(
[0] => GameShelf\UsersBundle\Entity\Ownership Object
(
[id:GameShelf\UsersBundle\Entity\Ownership:private] => 1
[type:GameShelf\UsersBundle\Entity\Ownership:private] => 1
[time:GameShelf\UsersBundle\Entity\Ownership:private] => 2010-02-05 11:00:00
[game:GameShelf\UsersBundle\Entity\Ownership:private] => Doctrine\ORM\PersistentCollection Object
(
[snapshot:Doctrine\ORM\PersistentCollection:private] => Array
(
[0] => GameShelf\GamesBundle\Entity\Game Object
(
[id:GameShelf\GamesBundle\Entity\Game:private] => 1
[parent_id:GameShelf\GamesBundle\Entity\Game:private] => 0
[name:GameShelf\GamesBundle\Entity\Game:private] => Somebody
[slug:GameShelf\GamesBundle\Entity\Game:private] => somebody
[reldate:GameShelf\GamesBundle\Entity\Game:private] => 2010-10-10
[genres:GameShelf\GamesBundle\Entity\Game:private] => 1,2
[platforms:GameShelf\GamesBundle\Entity\Game:private] => 1,2
[developers:GameShelf\GamesBundle\Entity\Game:private] => 1,2
[description:GameShelf\GamesBundle\Entity\Game:private] => Lipsum
[desc_src:GameShelf\GamesBundle\Entity\Game:private] => http://onet.pl
[rate:GameShelf\GamesBundle\Entity\Game:private] => 0
[ownership:GameShelf\GamesBundle\Entity\Game:private] => GameShelf\UsersBundle\Entity\Ownership Object
*RECURSION*
)
)
What I want, is to print name:GameShelf\GamesBundle\Entity\Game:private] => Somebody, but I don't know how. I use Twig, my current template is:
{% for game in games %}
{{ game.id }}
{% endfor %}
But it outputs only id from Ownership table.
My controller:
public function getOwnedAction($type = 1, $user = 1) {
$games = $this->getDoctrine()
->getRepository('GameShelfUsersBundle:Ownership')
->getOwned();
echo '<pre>';
//print_r($games);
echo '</pre>';
return $this->render('GameShelfUsersBundle:Default:index.html.twig',
array(
'games' => $games->getGame()
));
}
Repo:
public function getOwned($type = 1, $user = 1) {
$query = $this->getEntityManager()
->createQuery('
SELECT o, g FROM GameShelfUsersBundle:Ownership o
JOIN o.game g
WHERE o.type = :type
')
->setParameter('type',$type);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
It works only when I set getSingleResult instead of getResult.
In Twig you use the same methods to access Entity information.
[edit] After looking at the recursion, you might need to do it this way:
{% for owners in games %}
{% for game in owners.getGame() %}
{{ game.getId() }}
{{ game.getName() }}
{{ game.getParentId() }}
{# etc... #}
{% endfor %}
{% endfor %}
I have a Entity Field Type in a form, how do I check in Twig if its returning any value?
The entity field type:
$builder->add('users', 'entity', array(
'class' => 'UserBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
},
));
The correct variable to check was: form.users.vars.choices
So the code would be:
{% if form.users.vars.choices|length > 0 %}
{# actions #}
{% endif %}
Try this:
{% if users.options|length > 0 %}
{% for option in users.options %}
{# some action #}
{% endfor %}
{% endif %}