Twig Markup - Pulling out Variables - arrays

Im new to twig and having a hard time getting what I need out of an array.
Here is my output from
{{ dump(items) }}
array (size=1)
0 =>
array (size=2)
'content' =>
array (size=4)
'#type' => string 'processed_text' (length=14)
'#text' => string '8AS09DF8a90sd80' (length=15)
'#format' => string 'basic_html' (length=10)
'#langcode' => string 'en' (length=2)
'attributes' =>
object(Drupal\Core\Template\Attribute)[2249]
protected 'storage' =>
array (size=0)
...
So I have an object(i think) with nested info inside of it
I am trying to pull out the '#text' of 8AS09DF8a90sd80 but cant get anything but the dump to work.
Ive tried:
{{ dump(items[0]) }}
{{ dump(items['content']) }}
{{ items.content }}
{{ items.['content']['text'] }}
and a bunch of other formats, nothing works!!
How do I structure this in twig?

This should work:
{{ items.0.content['#langcode'] }}
We need to use:
0 to select the first key of the items array
content to select the content node
#langcode to get the value with #langcode key
Where . and [] have the same role: they are used to access to an attribute of an object, in this case the value associated to a key in an array. But writing items.0.content.#langcode would trigger a syntax error because # is not a valid character (1), so we have to use the other syntax ['#langcode'].
Source: Official documentation.
(1): I didn't tested but I'm pretty sure of this.

Related

Can't pass element from array to view

Trying to -unsuccessfully- pass as separated values into view from this array:
#items: array:1 [▼
0 => {#548 ▼
+"location": "Guadalajara"
+"location_id": 34
}
]
}
This array comes after a leftjoin query
$default_loc = DB::table('users')
->leftJoin('locations', 'users.location_id','=','locations.id')
->where('users.id',auth()->user()->id)
->select('location', 'location_id')
->get();
If I try to pass this array to the view via compact('default_loc') I'd been only able to show the entire array as shown above with {{ $default_loc }}, but can't separate it into $default_loc->location and $default_loc->id.
Thank you all,
Since $default_loc is an array, you should treat it as an array, so to access it you need to use the [] operator:
{{ $default_loc[0]->location_id }}
However, if you are sure that only 1 record will be returned, you can use the first() method instead of get() that will give you directly the object:
$default_loc = DB::table('users')
->leftJoin('locations', 'users.location_id','=','locations.id')
->where('users.id',auth()->user()->id)
->select('location', 'location_id')
->first();
...
{{ $default_loc->location_id }}
Is your expected result always 1 result? If yes you can change ->get() for ->first(), then you will have only a single Object.
If not, you need to iterate or access with index in the view:
#foreach($default_loc as $loc)
{{ $loc->location_id }} - {{ $loc->location }}
#endforeach
or
{{ $default_loc[INDEX]->location_id }} {{ $default_loc[INDEX]->location }}

Angular-js x-editable not populating the editable-number value on page load

I have x-editable form with two fields editable-text and editable-number.
%form{"editable-form" => "", :name => "editableForm", :onaftersave => "savePaymentDetails()"}
%table.table.table-striped
%tr{"ng-repeat" => "obj in bussiness_info.text"}
%td
%h6.blue
{{ obj.name | camelize}}
%td{"ng-if" =>"obj.type=='text'"}
%span{"e-name" => "obj.name", "e-ng-required" => "obj.required", "editable-text" => "obj.value"} {{ obj.value || 'empty' }}
%td(ng-if="obj.type=='number'")
%span{"e-name" => "obj.name", "editable-number" => "obj.value", "e-ng-required" => "obj.required","e-step" => "0.01","e-max"=>100} {{ obj.value || 'empty' }}
So,on clicking edit the form should populate both editable-text and editable-number fields if its values are present already.In my case both the fields the has the value,but only the editable-text alone is populated.
The editable-number is not getting populated with the pre-present value.I am not sure whether this because of any scope issues or something else.
I figured it out. Its my blunder. The typeof both tin_no(editable-text field) and margin(editable-number field) were String.So my editable-number field was not getting populated. Editable-number field got populated after changing the type of margin to Number.

Laravel 5 array validation

In laravel 5 how do i validate an array to check if at least one value is selected - the following doesn't work?
'user_list[]' => 'array|min:1',
{{ Form::select('user_list[]', $users, null , ['multiple' => 'multiple']) }}
fixed by changing the field name in the rules array as follows and by adding a required rule:
'user_list' => 'required|array|min:1',
{{ Form::select('user_list[]', $users, null , ['multiple' => 'multiple']) }}

How to change the separators for Cakephp Form Helper for multiple checkboxes

I'm generating a bunch of checkboxes through the Form Helper.
Essenciatially I have an array with like $tests = array
$tests = array(1 => 'test', 15=>'test2');
Then I can use it like this
echo $this->Form->input('test_id', array(
'type' => 'select',
'multiple' => 'checkbox',
'div' => false,
'before' => '<li>',
'after' => '</li>',
'separator' => '</li> <li>'));
I expected it would use the div => false to take off the div of every checkbox but it only applies the options to the exterior block. Is there anyway to change all the blocks from <div class=>'checkbox'> to <li class='anything else'>
Just look at the fields it produces, then write your own simple foreach() loop and write them yourself in whatever wrapping element(s) you want.
I forget whether it's possible with Cake or not, but don't think it is. The above is what we've done before - because it's simple to write, it took us less time to write than it did to look further into it :)

Remove empty value of select box

I have a selectbox like this
$form->select('city_id',$city, array ('empty'=>false, 'selected'=>'1',
'label'=> false), array('label'=>false, 'div'=>false, 'name'=>'city_id',
'id'=>'city_id'));
I need to remove the empty option at the top of my options. I even set the
''empty'=>false' but it not works!!!
Can anyone help me please
It looks like you've messed up your arguments to $form->select().
The 1st is the field name, the 2nd is an array of key/values of select options for the user to pick from, 3rd argument should be the selected element value (or null) and the 4th the array of options, which is where you can include 'empty' => false.
select(string $fieldName, array $options, mixed $selected, array $attributes)
See the select documentation in the CakePHP Cookbook.
<?php echo $this->Form->input('foo.bar', array('type' => 'select', 'options' => array(1 => 'foo', 2 => 'bar'), 'empty' => false)); ?>
Works for me..

Resources