0

I'm converting json to larvel php and I'm wondering how you convert this:


JSON:

"assetList":[
  {
    "name":"https://mobilize-uploads-prod.s3.amazonaws.com/uploads/event/Test-Logo-Small-Black-transparent-1_20180730154641244030.png"
  },
  {
    "name":"https://mobilize-uploads-prod.s3.amazonaws.com/uploads/event/Test-Logo-Small-Black-transparent-1_20180730154641244030.png"
  },
  {
    "name":"https://mobilize-uploads-prod.s3.amazonaws.com/uploads/event/Test-Logo-Small-Black-transparent-1_20180730154641244030.png"
  }
]

larvel php:

'thumbnail' => 'assetList'['name'],

Your answer will look something like this:

 'thumbnail' => 'Your code here',

It is supposed to output an image there is 3 different images I just used one as an example. Right now, it is not outputting anything so it is not working maybe because it is an array and I'm not sure how you write an array like this to work. I think the array is stopping it from working. Any help would be much appreciated.

edit: I just found out that this is larvel php

11
  • 1
    I'm sorry, I don't understand. Do you mean you want to use the json in the template or your twig template is supposed to generate the same output? Can you maybe explain some more? Commented Oct 18, 2018 at 20:21
  • it is an array and I don't know how to write an array in twig php Commented Oct 18, 2018 at 20:53
  • I have now tried this: assetList'[array ('name')], Commented Oct 18, 2018 at 21:15
  • still does not work Commented Oct 18, 2018 at 21:15
  • Can you pls tell me which Framework are you using with Twig Commented Oct 19, 2018 at 4:55

3 Answers 3

2

Please see my answer below

  1. First Your Json should be as below

{"assetList":[
  {
    "name":"https://mobilize-uploads-prod.s3.amazonaws.com/uploads/event/Test-Logo-Small-Black-transparent-1_20180730154641244030.png"
  },
  {
    "name":"https://mobilize-uploads-prod.s3.amazonaws.com/uploads/event/Test-Logo-Small-Black-transparent-1_20180730154641244030.png"
  },
  {
    "name":"https://mobilize-uploads-prod.s3.amazonaws.com/uploads/event/Test-Logo-Small-Black-transparent-1_20180730154641244030.png"
  }
] }

  1. First Do a Json_decode in php for your data.

    $json_data = [YOUR_JSON_DATA]
    $data_arr['data'] = json_decode($json_data,true);
    
  2. Pass the array to twig // for Symfony php framework

    $this->render("app:index.html.twig", $data_arr);
    
  3. In your twig you need to update the code as below to show the Image urls

    {% for img_data in data.assetList %}
        {{ img_data.name }}
    {% endfor %}
    

Let me know if you face any more issue.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to build a similar structure to the JSON inside Twig, you can do this as follows:

{% set thumbnail = { "assetList": [{"name": "http://..."}, {"name": "http://..."}] } %}

If you want to pass a similar structure into the view, e.g. do something like $this->render('my_template', ['thumbnail' => ...] then your php code to generate an array like the one in JSON could look like this:

[
    'assetList' => [
        [
            'name' => 'http://...',
        ],
        [
            'name' => 'http://...',
        ],
    ]
]

edit: Also accessing it in Twig will not be assetList["name"] it will be:

{{ thumbnail["assetList"][0]["name"] }}

Where 0 is the offset of the list entry,i.e. 0-2, if you have 3 images.

Comments

0

If you passed that json as an array towards twig, u would need to use a loop to read out the array assetList

{% for asset in assetList %}
    {{ asset.name }}
{% endfor %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.