I'm trying to print out a table with keys and values from a 2d-array in twig. The only issue is, that I'm trying to get the values and keys dynamically printed depending on the twig result I get back.
The array I'm getting back from another function can be different with every call, so my goal would be to write a function once instead of multiple times depending on the typ of data I recive.
The array I get is in {{ tableContent }}.
One example of the values are:
{
["Type"] => string(4) "2021"
["Description"] => string(11) "Stundenlohn"
["Symbol"] => string(3) "ABC"
}, {
["Type"] => string(4) "2024"
["Description"] => string(9) "Something"
["Symbol"] => string(3) "XYZ"
}
so in this case I want the table to look something like:
# | Type | Description | Symbol
--+------+-------------+--------
0 | 2021 | Stundenlohn | ABC
1 | 2024 | Something | XYZ
this would work quite well, if I always know the names of the array keys:
{% for key, u in tableContent %}
<li>{{ key }}: {{ u.Type }}</li>
{% endfor %}
And with this I'd get 0: 2021 and 1: 2024
is there a way to not use (in my case) Type but somehow the index?
With this solution I'm working on at the moment I only get the first value:
{% for array in tableContent %}
{% for id, key in array %}
{{id }} | {{key}}<br/>
{% endfor %}<br/>
{% endfor %}
I need to create a array with a dynamic key.
Example:
{% set key = 'a' %}
{% set value = 'b' %}
{% set array = {key:value} %}
I find a workaround for this problem: if you surround the key with parenthesis the key of array take the value of the variable instead of the name. So try simple this:
{% set array = { (key): value} %}
Here a working solutions
I have a multi dimensional array along the lines of
array(2) {
[11]=> array(1) {
["L2J"]=> array(1) {
["VS7"]=> array(2) {
["26 Feb 2015 12:00"]=> array(2) {
["C"]=> string(1) "9"
["D"]=> string(1) "9"
}
["26 Feb 2015 13:00"]=> array(2) {
["C"]=> string(1) "9"
["D"]=> string(1) "6"
}
}
}
}
}
Now I have done some looping and I am now at the point where I have access to the dates.
{% for sid, psuedos in alerts %}
{% for psuedo, flights in psuedos %}
{% for flight, dates in flights %}
{% endfor %}
{% endfor %}
{% endfor %}
Now I am converting some normal PHP code and at this point, I would do
$firstDate = array_pop(array_keys($dates));
Is there any way to do something like this in Twig? I have searched about but cant seem to find anything.
Update
This is my latest effort, can't seem to get it to slice the last array element though
{% set firstDate = [dates|keys]|last|slice(1) %}
There isn't a Twig function that will do exactly what array_pop() does (return the last array element and shorten the array at the same time), but there are ways to do them separately.
Given:
{% set array = [1,2,3,4,5] %}
To get the last element, use Twig's last filter.
{{ array|last }}
{# returns '5' #}
You can remove only the last element with the slice filter like this: slice(0,-1)
{% set array = array|slice(0,-1) %}
{# array = [1,2,3,4] #}
... or the Craft without filter:
{% set arrayLast = array|last %}
{% set array = array|without(arrayLast) %}
{# array = [1,2,3,4] #}
pop last element
{% set array = [1,2,3] %}
{% set value = array|last %}
{{ value }} {# return 3 #}
{% set array = array|slice(start, length - 1) %}
{% for value in array %}
{{ value }} {# return 1,2 #}
{% endfor %}
pop the first element
{% set array = [1,2,3] %}
{% set value = array|first %}
{{ value }} {# return 1 #}
{% set array = array|slice(start + 1, length) %}
{% for value in array %}
{{ value }} {# return 2,3 #}
{% endfor %}
Just as a quick check, have you tried slice(-1)?
Passing a negative number as the first parameter should start at the end of the array and work that many back.
I there,
I'm trying to find the last character for a string in twig.
What I need to do is that if the string ends with s then I only add ' Zone and if the string doesn't have an s at the end I should add 's Zone.
E.g., "Charles's Zone" should become "Charles' Zone".
Thanks a lot
{% set test_string = 'asdfs' %}
{% set test_string = test_string ~ (test_string|last == 's' ? "' " : "'s ") ~ "Zone" %}
using last
{{ [1, 2, 3, 4]|last }}
{# outputs 4 #}
{{ { a: 1, b: 2, c: 3, d: 4 }|last }}
{# outputs 4 #}
{{ '1234'|last }}
{# outputs 4 #}
I figured a workaround:
{% set title = user_name ~ "\'s zone" %}
{% set replace_value_var= "s\'s zone" %}
{% set replace_with_value_var = "s\' zone"%}
{% set MyTitle = title|replace({ (replace_value_var): replace_with_value_var }) %}
http://twig.sensiolabs.org/doc/filters/slice.html
{% set varTest = 'azertyuiop' %}
{{ varTest[:2] }} {# show 'az' #}
but on an object as {{ myObj.name[:2] }} result is Array !
is-it the filter limit ?