I am trying to access the value in twig from:
image of data dump
But keep getting an error:
Impossible to access an attribute ("startDate") on a integer variable ("2").
And in my twig it's:
{% for item in data %}
...
{{ item.decoded.startDate }}
...
In Controller:
foreach ($data as $d) {
$d->decoded = json_decode($d->getValue());
}
Can somebody help skipping the error when the value is integer or null?
Try to dump item.decoded to see if startDate is available
Related
I hav text filed where I stored some json data.
I am getting in my twig template on dump like:
"{"birthDate":"2021-09-01", "amount":8}"
I want to access keys so I can extract it's values.
Tried with json_encode or:
{% for key, value in item.value %}
but nothing seems to work.
Is the problem with the output data?
It seems that your item is neither object nor array it seems to be a string.
Try to convert your data to object or array json as below:
$objJson = json_decode($yourDBArray['menu']);
$arrJson = json_decode($yourDBArray['menu'],true);
Then you can use to show as below
{% for key,value in menu %}
Key : {{ key }}
Value : {{ value }}
{% endfor %}
How can I Use {{dump(variable_name)}}
Here if result is a variable
<pre>
{{dump(result)}}
</pre>
php
->get('field_example') and ->field_example
are the same and so is TWIG
.get('field_example') and .field_example
both get the field object (field item list) from which you can get the field value(s).
{{ content.field_example }}
hope answer the questions
I'm working on a laravel project.
now I want to access one of my column which is data
The data colmn contains an array of stuff like message, klant_address, reactionPlacer and more.
foreach(Auth::user()->unreadNotifications as $notfiy) {
{{ $notify->data['klant_address'] }} //This gives an error "variable is not assigned"
}
but if i store it first to a variable it does'nt gives an error like this
foreach(Auth::user()->unreadNotifications as $notfiy) {
$klantAddress = $notify->data['klant_address']
echo $klantAddress
}
Now comes the weirdest part of all
if i do
dd($notify->data['klant_address']) <-- this does gives me the signle string return
and
foreach(Auth::user()->unreadNotifications as $notfiy) {
{{ $notfiy->data['messages'] }} <--- This does output
}
does work.
I hope someone can help me or at least explain why this is happened. Because I'm so confused for it.
thanks in advance.
Error Page
Database Table
Looks more like JSON to me. (Look at the last image)
Try this:
{{ $notify->data->klant_address ?? '' }}
If that doesn't work, try {{ json_encode($notify->data)->klant_address ?? '' }}
(Thrown in ternaries to help with nulls etc.)
I am using Twig and I have a problem.
I have a problem when I want to use a variable index for an object.
Here is my code:
{% for label, field in params.fields %}
{{ dump(data.field) }}
{% endfor %}
data is an object containing {'email': 'test#test.fr', 'name': 'John'}.
Field is an array of string containing ['email', 'name']
I can't show the value my object dynamically.
{{ dump(data.email) }} works.
How can I use dynamic indexes?
In Twig syntax, data.field is equal to $data['field'] in PHP. In other words, Twig use field as the array key name instead of taking the value of the field variable and use it as a key name.
If you want something similar to $data[$field], you can use the attribute() function:
The attribute function can be used to access a "dynamic" attribute of a variable:
Example:
{{ dump(attribute(data, field)) }}
{# or simply #}
{{ attribute(data, field) }}
i'm trying to make a loop in php while using twig and inside this loop,
i am creating a parameter which contains the record from the database query.
The problem is that when I use the parameter in my HTML file, it only return 1 record from the while loop, even if there are 3 or 4 or even more..
This is the php code I have:
public function getWidgetsByName($name)
{
global $params;
$get = $this->db->query("SELECT * FROM profile_items
WHERE category = 'widget'
AND username = '". $name ."'");
if($get)
{
while($key = $get->fetch())
{
$params["profile_widget_name"] = $key['name'];
}
}
}
And this is the HTML parameter in my HTML twig rendered file:
{{ profile_widget_name }}
The paremeters just get rendered how they are supposed to be rendered:
echo $twig->render('app/views/'. $_REQUEST['p'] .'.html', $params);
And yes the $params variable is an array, in the config it file it first gets used as $params = array("..." => "..."); and I add things to this array by doing $params["..."] = "...";
So, I hope someone can help me.
Thanks in advance,
Best Regards.
At the moment, the value of $params["profile_widget_name"] is just one string. Every time you go through the while loop, you overwrite the previous value of the key with the current value.
So when you pass $params to Twig, the value of profile_widget_name is the value of name in the last row of the database to be selected.
I think what you want instead is for the value of profile_widget_name to be an array. Then every time you go through the loop, the current value of name is added to the array, instead of overwriting it.
You do this by doing something like:
$params["profile_widget_names"][] = $key['name'];
Now in your Twig template, you'll need to do something like:
{% for profile_widget_name in profile_widget_names %}
{{ profile_widget_name }}
{% endfor %}
Using Multiple Parameters
If you want multiple parameters to be on there, you can do this:
$params["profile_widgets"][] = [
'pos_x' => $key['pos_x'],
'name' => $key['name'],
];
And in Twig:
{% for profile_widget in profile_widgets %}
Name: {{ profile_widget.name }}
Pos X: {{ profile_widget.pos_x }}
{% endfor %}