my view page consist {{$data->attachment}} which render
[{"filename":"hello.jpg", "location":"/home/my_folder"}].
Here i tried to display file name in view page using
#foreach($data->attachment as $attachment)
$attachment->filename
#endforeach
which gives me
Invalid argument supplied for foreach()
i trying
{{$data->attachment->filename}}
which gives me
Trying to get property of non-object
what i'm doing wrong? how can i display filename? thanks.
As you mentioned in the comments, the value of $data->attachment is actually a string. In order to iterate over it in a loop you will need to convert it back to an array.
So if you change your loop to:
#foreach(json_decode($data->attachment) as $attachment)
{{ $attachment->filename }}
#endforeach
you should get what you want.
I would add that the correct place for this conversion should really be in your controller, not your view.
Your question was not entirely clear so this is all assuming you knew what you were doing when you used a foreach loop, meaning you actually have multiple entries in the $data->attachment array. If it is just the one attachment and you are really just trying to get the filename, then you don't need a loop, all you need is to say:
{{ json_decode($data->attachment)->filename }}
and again I would add that really that all belongs in your controller so that in your view you would end up with something like {{ $filename }}
Related
I don't know if the terminology is correct, but essentially it comes down to using something like site.GetPage or path.Base as opposed to something like humanize with apply in Hugo:
I have a part of a layout in Hugo that looks like the following:
{{ range .Params.related_plays }}
{{ with site.GetPage . }}<li>{{.Title}}</li>{{ end }}
{{ end }}
where related_plays is a list of strings containing the path to pieces of content (e.g. ["english/play-1.md", "english/play-2.md"]. This currently works.
Instead of the above, I'd like to assign the list of page objects into a variable. I've tried something like the following:
{{ $related_plays := apply .Params.related_plays "site.GetPage" "." }}
but I get the following error:
error calling apply: can't find function site.GetPath
Is there any way to call apply for something like this?
I'm guessing my alternative is to build up a Scratch slice via the range and with functions, but if it can be done with apply, I would prefer that.
I'm trying to convert an Object array into an array without use "let item of array" on html, I already google a lot but nothing that I find works.
Why I don't use loops? because I pretend to display data inside a page that comes from a liteSQL database, so all the data that I extract from that it's an Object and sure I can display the data without issues if I use a loop like "let item of array" but in this case I just want to show information on the HTML like item.name or item.avatar
Thanks in advance for any help, If you guys need more information please let me know.
The database have students so every array have: name, age, avatar, etc, so I try to show some like a profile after they tap the name on the list
EDIT:
What you are receiving from your server is an array of objects.
array of objects ->
[{
key1:vlaue1,
key2:value2
},
{
key1:vlaue1,
key2:value2
}]
You refer the values here by using array[0]['key1'] array[0]['key2'] and so on
In your case, you are receiving only one object in the array, so simply use array[0].name array[0].age and so on
Thanks for the help, I actually found the solution by my self, I just need to use:
item: array = <array>{};
in order to use my object array as a simple array items without any loop on html
I am trying to iterate through two array in my view in Angular2. I iterate through my first array using *ngFor and I use the index to iterate through the second one. The problem is, I can't get the attribute of an object of the second array, it's just bug everything.
<tr *ngFor="let round of rounds ; let i = index">
<td>{{customers[i].login}}</td>
<td>{{round.status}}</td>
</tr>
Here, the customers[i].login doesn't work. But if instead I put only customers[i] I'll see in my view that I have [object Object].
How can I access the attribute of my customer object, or how can I iterate through the two array at the same time in a better way ?
customers[i]?.login
You can use the ?. accessor in order to prevent the error from happening.
The accessor variant of the existential operator ?. can be used to
soak up null references in a chain of properties. Use it instead of
the dot accessor . in cases where the base value may be null or
undefined.
I'm using an analog of Shopify and I'm stuck with syntax of Liquid.
I need to output in the template the field with an id product[product_field_values_attributes][][value]
So I need to write a loop to get the i value of this array.
I'm confused with this empty element in the brackets.
I've looked through the examples of loop syntax in Liquid but all of those arrays are simple and they are not my case.
For example, the Title field has id product[title] and in Liquid template i call this variable product.title and it works fine.
But all my tries to write a loop for this array failed.
Please, help to write a loop to get the values of the array stated above.
Try outputting the array directly onto the page using {{ product }} or {{ product[product_field_values_attributes] }} somewhere in the HTML. That will do a JSON-like string representation of the array. From there you can figure out what the keys for the array are.
I'm not sure what you're saying about the i value. You don't reference i anywhere else in your question. If you can clarify that then we can see if something can be done about it.
I have been trying to access the values of the the array .. in the foreach loop but no luck, the first two calls get the values, but the ones in the foreach loop nothing.. comes empty although if i print the array all the values are there.
You never actually access the $dish variable in the foreach loop.
Try
$dish['Dish']['dish_name'];
instead of
$dishes['Dish']['dish_name'];
etc.
Additionally, you will get an error because the first element in your array is not a Dish, its a Dish_Category. Thus, either remove this element from the array, or use a simple if statement before you access the $dish such as:
if($dish['dish_name'])
//DO STUFF HERE
Also, there is no reason for so many
<?php ?>
tags. Couldn't you put the whole block of code in one?