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 }) %}
Related
Is there an equivalent to this in Twig :
<?php
$a = 'hello';
$b = '<h1>'.$a.'</h1>';
echo $b;
?>
I try this but without success :
{% set a = 'hello' %}
{% set b = <h1>{{a}}</h1> %}
I'm new to twig and couldn't find a way to do this.
To concatenate in Twig you need the tilda symbol, ~
Eg.
{% set a = 'hello' %}
{% set b = '<h1>'~a~'</h1>' %}
Or you can use string interpolation
E.g.
{{ "<h1>{a}</h1>" }}
You can concatenate and dump with the raw filter:
{% set a = 'hello' %}
{% set b = '<h1>' ~ a ~ '</h1>' %}
{{ b|raw }}
Here a working solutions
{%for mat in setQuery %}
{% set datePost = mat.data_criacao|date('d-m-Y') %}
{% set today = "now"|date('d-m-Y') %}
{{today- datePost}}
{% endfor %}
datePost = 17-04-2015
today = 06-05-2015
the example above returns it: -11
The issue was resolved with the following code:
{% set datePost = mat.data_criacao|date('d-m-Y') %}
{% set today = "now"|date('d-m-Y') %}
{% set difference = date(today).diff(date(datePost))%}
{% set leftDays = difference.days %}
{% if datePost == today %}
1 day
{% else %}
{{ leftDays }}
{% endif %}
You must write your custom twig extension:
You must write a twig function as described here with the following code for make diff via php function:
$calcFrom = $from;
$calcTo = $to;
$now->diff($calcFrom)->format("%a")
And make it available via a Twig extension.
If you are using symfony2 framework You can use the KnpTimeBundle
In the Twig:
This compare with the current date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(form) }}
This compare with the another date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(form , to ) }}
Hope this help
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 have a code TWIG :
{% set foo = 1 %}
{% set items = [foo] %}
let me ask why :
{% set items = [foo] %}
does not work?
Please let me know the reason and how to solve it.
I want it to work in [] .
Thank you.
The following code works with the most recent version of twig:
{% set a = 30 %}
{% set b = 60 %}
{% set c = a + b %}
{% set d = [a,2,3,b,c] %}
a: {{a}} {# echos 30 #}
b: {{b}} {# echos 60 #}
c: {{c}} {# echos 90 (a+b) #}
d[0]: {{d[0]}} {# echos 30 (a) #}
d[1]: {{d[1]}} {# echos 2 #}
d[2]: {{d[2]}} {# echos 3 #}
d[3]: {{d[3]}} {# echos 60 (b) #}
d[4]: {{d[4]}} {# echos 90 (c) #}
{% set e = {a: 15} %}
e.a: {{e.a}} {# echos 15 #}
I guess your problem is about how to access the values, so I included some of the accessing methods of twig.
I hope this solves your problem.
I want to do something like this:
{% set c=a+b %}
Where a and b are strings.
How can I do it?
The way to do it is:
{% set c = a ~ b %}
Use the "~" operator. This will concatenate your two strings. The "+" operator cannot be used to combine strings.
You would get this:
{% set c=a~b %}
More info:
The "+" operator: Adds two objects together (the operands are casted to numbers).
You can use:
{{ "Hello " ~ name ~ "!" }}
A clearer example for the {% block page %}...{% endblock %}:
{% block page %}
{% set page = page | merge({
"title" : branchName,
"description" : "This description has "~branchName~" as its title"
}) %}
{{ parent() }}
{% endblock %}
A clearer example for the {% block content %}...{% endblock %}:
{% block content %}
This is just a sample string for {{ branchName }} that needs no concatenation
{% endblock %}