0

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>

or

{% 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.

Danish
  • 1,467
  • 19
  • 28

2 Answers2

0

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 %}
dragontree
  • 1,709
  • 11
  • 14
  • hi thank you for answer i tried that one as well but value results the whole array like this ``` 0: {"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}``` – Danish Jun 18 '17 at 15:43
  • Are you sure its an object, not a json string? – dragontree Jun 18 '17 at 15:45
0

So the answer is rather complex then even i anticipated! i had to do a lot of digging with frustration to really get at the bottom of this matter. First thing first, i was doing a cron job where i saved the data from a model in text type field. That is why if you see above result i.e

{% 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}"]

it outputs a JSON String, too bad can't iterate or do something with it.

To solve this,

  • Create json_decode filter in twig.

  • Apply the filter to value part of array.

  • Access Individual values of array with variable[keyname] method.

I created a twig filter json_decode for creating filter see this Link

while in October, the creation to new twig extension is rather easy which is just give registerMarkupTags method in Plugin.php with filter array poiting to name and function name. See this link for extending twig in octobercms here

Now, the part we were waiting for, how to get the values and show them in twig template. Here it is going to be, by using above same example. This is what i did

{% set wordoftheday = __SELF__.words %}
{% for key, value in wordoftheday %}
  {% set decoded = value|json_decode %}
  # to get the indvisual values
  {{ decoded['id'] }} 
  {{ decoded['created_at'] }} 
{% endfor %}
Danish
  • 1,467
  • 19
  • 28