Twig iterate / read and get value - Octobercms
source link: https://www.codesd.com/item/twig-iterate-read-and-get-value-octobercms.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Twig iterate / read and get value - Octobercms
Hi guys its rather a very basic question, had chance to look several questions on stackoverflow but all in vain.
so i have this twig variable called "WordoftheDayfromDB ", to which i am passing from some data after querying DB in my controller via laravel pluck
method. The controller exsits in plugin of octobercms. the content of the variable is shown below
{% set WordoftheDayfromDB = __SELF__.words %}
{{WordoftheDayfromDB}} # this output below object
["{\"id\":4,\"word_tr\":\"parazit\",\"slug_tr\":\"parazit\",\"word_gr\":\"\\u03c0\\u03b1\\u03c1\\u03ac\\u03c3\\u03b9\\u03c4\\u03bf\",\"slug_gr\":\"parasito\",\"pubordraft\":1,\"created_at\":\"2017-06-07 13:04:57\",\"updated_at\":\"2017-06-07 13:04:57\",\"deleted_at\":null,\"word_image\":\"\\\/cropped-images\\\/image2.jpg\",\"typeswb_id\":0}"]
can someone tell me a way to extract keys and values from the about twig variable. what i already tried is following:
<pre> {{WordoftheDayfromDB.id}}</pre>
{% for item in WordoftheDayfromDB %} {{item.word_tr}} {% endfor %}
also some combination using {% if WordoftheDayfromDB is iterable %}
. I will appreciate your answer very much! thank you for reading my question.
You can use the for
loop so that the keys and values are both accessible like this:
{% for key, value in WordoftheDayfromDB %}
<li>{{ key }}: {{ value }}</li>
{% endfor %}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK