Array
(
[0] => Array
(
[product_id] => 49
[thumb] => http://192.168.0.101/user1/OPC_Fabish3.0/upload/image/cache/catalog/demo/banners/Productimage-278x355.jpg
[rollover_thumb] => http://192.168.0.101/user1/OPC_Fabish3.0/upload/image/cache/catalog/demo/banners/Productimage-278x355.jpg
[name] => tulip lamp family
[description] => Samsung Galaxy Tab 10.1, is the world’s thinnest tablet, measuring 8.6 mm thickness, running w..
[price] => $241.99
[special] =>
[tax] => $199.99
[rating] => 0
[href] => http://192.168.0.101/user1/OPC_Fabish3.0/upload/index.php?route=product/product&product_id=49
[model] => SAM1
)
)
This is my products array in PHP.
In twig I am fetching the values using the following code:
{% for j in 0..(products|length) %}
{% endfor %}
However I am not getting any result.
I suppose it is:
// or
Or simplier:
{% for product in products %}
{% endfor %}
Related
Array ( [status] => 1
[data] => Array (
[61d2f1fef27db255838d2c59] => Array (
[name] => General
[questions] => Array (
[0] => Array (
[id] => 61d54149c98b65a672acf79b
[question] => What will happen to posts that I made on my classic Page?
[answer] => All of your previous posts will be preserved on your Page, and insights will still be available for them.
)
[1] => Array (
[id] => 61d2eb5db97dcc3c488f0ef9
[question] => How can I add my Google Analytics id in for the mobile website ?
[answer] => Before starting, Please keep your Google Analytics id ready with you. Go to the Settings > Manage Design > Data Settings and enter your id in the analytics id field & save. Please refer to the video for step by step help.
)
)
)
[61d53eb4c98b65a672acf798] => Array (
[name] => Other
[questions] => Array (
[0] => Array (
[id] => 61d54131c98b65a672acf79a
[question] => How can I open my new Page?
[answer] => To open your new Page on desktop: From your home page, click on your profile picture in the top right. Select Switch Profile and select the new Page.
)
[1] => Array (
[id] => 61d53fc9c98b65a672acf799
[question] => What is your return policy?
[answer] => We allow returns of all items within 30 days of your order date. Just send us an email with your order number and we will send you a return label.
)
)
)
)
)
I am new to twig and i have got this data and want to access the every [question] and [answer] data. Please someone help me in order to extract question and answer using twig looping statements.
{% for data in api_response %}
{% for keys in data %}
{{ keys }}
{% endfor %}
{% endfor %}
I tried it doing this way but i am not getting any output
At this moment you are only using one loop, but as we can see, the questions are stored in another array. Meaning you'll have to loop that array as well
{% for row in result.data %}
<h2>{{ row.name }}</h2>
{% for data in row.questions %}
<p>{{ data.question }}</p>
{% endfor %}
{% endfor %}
demo
Edit from comment:
{% for row in result.data %}
{% if row.name|lower = 'general' %}
<h2>{{ row.name }}</h2>
{% for data in row.questions %}
<p>{{ data.question }}</p>
{% endfor %}
{% endif %}
{% endfor %}
5 days have gone by, still no success!
I dumped the results of my card variable and it returns results.
But when I try to pass it trough varibale on my twig tamplate it throws:
Variable "card" does not exist.
It's defined in SonataAdmin.
protected function configureShowFields(ShowMapper $showMapper)
{
$card = $this->getCardTransactions(); // on dump(), it works
$showMapper->tab('Cards')
->add('Data', 'string', array(
'template' => "#AdminTemplates/sonata/details.html.twig",
'card' => $card
))
->end()
->end();
}
and in my twig;
{% for c in card %}
{{ c.id }}
{% endfor %}
I think it has to do with SonataAdmin and how it handles this type of calls but I have read the documentation and searched online but still no luck.
You have to use the field_description.options object in your template to access your variable.
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->tab('Cards')
->add('Data', 'string', [
'template' => "#AdminTemplates/sonata/details.html.twig",
'card' => $this->getCardTransactions(),
])
->end();
}
{# #AdminTemplates/sonata/details.html.twig #}
{% extends '#SonataAdmin/CRUD/base_show_field.html.twig' %}
{%- block field -%}
{% spaceless %}
{% for card in field_description.options.cards %}
{{ card.id }}
{% else %}
<p>No card</p>
{% endfor %}
{% endspaceless %}
{%- endblock -%}
I am new to Twig. I thought I had mastered the basics but am completely stuck on this.
I have two arrays:
books => [
[
'book_id' => ...,
'book_title' => ...,
],
],
and
tags => [
[
'tag_id' => ...,
'book_id' => ...,
'tag' => ...,
],
],
How do I get Twig to produce something along these lines
Book title 1 tag1 tag2 tag3 tag5
Book title 2 tag1 tag3 tag4 tag2
and so on
So far I have
{% for book in books %}
{{book.title}}
{% for tag in tags %}
what goes here to get a list of tags for this book
{%endfor%}
{% endfor %}
I know it is only a few lines of code but I don’t know where to start
A hint in the right direction would be really appreciated.
So you know the book_id. You should be able to use this to limit your FOR loop over the Tags:
{% for book in books %}
{{book.title}}
{% for tag in tags if book.id == tag.id %}
{{ tag.tag }}
{%endfor%}
{% endfor %}
See: http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition
I am new to the Symfony2 framework and am trying to parse some XML from the lastfm API and display information to the user. this would be in the format of album title, playcount and album image for each item.
I can display all this information so far to the user but this is not really useful as I intend to add CSS styling to my page. Any suggestions would be appreciated.
This is my Controller
/**
* #Route("/lastfm/albums", name="albums")
* #Template()
*/
public function albumsAction()
{
$albumsclass = new Album();
// pull in artist albums
$albums = simplexml_load_file('http://ws.audioscrobbler.com/2.0/? method=artist.gettopalbums&artist=imagine+dragons&api_key=370f98844440c2ecc8e5f7 c6cea8a7a4');
$rank = $albums->xpath('/lfm/topalbums/album/#rank');
$album_name_array=array();
$album_playcount_array=array();
$album_url_array=array();
$album_image_array=array();
foreach ($rank as $ranks){
foreach ($ranks as $rank_id) {
$album_name = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/name');
$album_playcount = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/playcount');
$album_url = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/url');
$album_image = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/image[4]');
}
$album_name = implode($album_name);
array_push($album_name_array,$album_name);
$album_playcount = implode($album_playcount);
array_push($album_playcount_array,$album_playcount);
$album_url = implode($album_url);
array_push($album_url_array,$album_url);
$album_image = implode($album_image);
array_push($album_image_array,$album_image);
}
$container=array();
for($i=0; $i<sizeof($album_name_array); $i++) {
array_push($container,$album_name_array[$i],$album_playcount_array[$i],$album_ur l_array[$i],$album_image_array[$i]);
}
//$hello = array('album_name'=>$album_name_array,
// 'album_playcount'=>$album_playcount_array,
// 'album_url'=>$album_url_array,
// 'album_image'=>$album_image_array,);
//array_push($album_name_array,$album_playcount_array);
return $this->render('AcmelastfmBundle:Default:albums.html.twig', array(
// 'pageData' => array(
// 'artistxml' => $artistxml,
'rank' => $rank,
'ranks' => $ranks,
//'rank_id' => $rank_id,
// 'ranks' => $ranks,
'album_name' => $album_name_array,
//'album_playcount' => $album_playcount_array[$i],
'album_url' => $album_url_array,
'album_image' => $album_image_array,
'container' =>$container,
'data' => var_export($container, true),
//
// 'hello' => $hello,
// 'james' => array('album_name' => $albumsclass->getAlbumName()),
// ),
));
}
This is my view
{% extends '::lastfmbase.html.twig' %}
{% block title %}Albums{% endblock %}
{% block body %}
{% for key in container %}
{{key}} <br>
{% endfor %}<br>
{% endblock %}
I am basically trying to convert this code in PHP to symfony2. However I cannot find a way to pass the associative array values to twig as I get an array to string conversion error
<?php
// pull in artist albums
$albums = simplexml_load_file('http://ws.audioscrobbler.com/2.0/? method=artist.gettopalbums&artist=imagine+dragons&api_key=370f98844440c2ecc8e5f7 c6cea8a7a4');
$rank = $albums->xpath('/lfm/topalbums/album/#rank');
foreach ($rank as $ranks){
foreach ($ranks as $rank_id) {
$album_name = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/name');
$album_playcount = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/playcount');
$album_url = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/url');
$album_image = $albums->xpath('/lfm/topalbums/album[#rank="'.$rank_id.'"]/image[4]');
}
$album_name = implode($album_name);
$album_playcount = implode($album_playcount);
$album_url = implode($album_url);
$album_image = implode($album_image);
print_r($rank_id);
?>
<article class="album">
<?php
echo "".$album_name."<br>";
echo $album_playcount." listeners<br>";
echo "<div><img src=\"".$album_image."\" title=\"$album_name\" /></div><br>";
?>
</article>
<?php
}
I am not sure exactly what you are asking.
Do you mean this?
{% for key, value in container %}
{{ key }}: {{ value }}
{% endfor %}
I have array in PHP:
array(
'name1' => 'one',
'name2' => 'two',
'name3' => 'three'
)
but in twig:
{% for i in range(1, 3) %}
{{'name' ~ i}}
{% endfor %}
give me:
1
2
3
Please help me. Thanks.
You can do this:
{% for i in range(1, 3) %}
{{ attribute(_context, 'name' ~ i) }}
{% endfor %}
To have:
one
two
three