Intval in Twig Template
Asked Answered
A

4

28

The problem is very short.

I have got an array with integer keys, and parameter which is the string. I want to get element of that array by using that parameter.

How to get in Twig intval - (int)"32" of {{ variable }} where variable == (string) "32";?

Arpent answered 30/6, 2014 at 11:18 Comment(1)
Are you sure this isnt application logic and should be done before entering twig?Checkrow
C
29

There are filters in twig. One of them is this:

https://twig.symfony.com/doc/2.x/filters/number_format.html

{{ variable|number_format }}

if you are doing math on a string, it will automatically cast to an interger anyways, however!

Checkrow answered 30/6, 2014 at 12:0 Comment(8)
Correct me if I'm wrong, but here you are inversing OP's problem. {{ variable|number_format }} is converting an int to string, which yes, solves the problem, but the other way around...Amaurosis
I do not believe this is correct. I just tested it and "number_format" filter does not convert string to int.Uninterrupted
as @Amaurosis mentioned correctly, this is converting a float into a string. See php.net/number_format as well as the twig function is just a wrapperConduplicate
You are correct. My answer assumes that the problem occured while displaying the value, not while applying logic to it.Checkrow
This is actually the right answer, number_format converts a string to a number.Madaih
It partly works. It converts a string into a numeric string (but it's still technically a string).Cheadle
Lindsey D and others are right, it converts variable to string. In the Twig documentation they state "It is a wrapper around PHP's number_format function", which in fact returns string. Ref.Boron
The function's purpose is to format a number, which means there will be a string as the result and moreover you can get it with thousands separator, e.g. instead of expected 1000 you'll get '1,000'Accept
I
9

You can access a value of an array using a variable as the key like this: myArray[myKey]. It doesn't matter whether the array keys are integers (e.g. 32) or corresponding strings ("32"), or whether myKey is an integer or a string. All of these combinations should work (see TwigFiddle):

{% set myArray = {
    1:   'first',
    '2': 'second',
} %}

{% set keyOneInt = 1 %}
{% set keyOneStr = '1' %}
{% set keyTwoInt = 2 %}
{% set keyTwoStr = '2' %}

{# These all print 'first' #}
{{ myArray.1 }}
{{ myArray[1] }}
{{ myArray['1'] }}
{{ myArray[keyOneInt] }}
{{ myArray[keyOneStr] }}

{# These all print 'second' #}
{{ myArray.2 }}
{{ myArray[2] }}
{{ myArray['2'] }}
{{ myArray[keyTwoInt] }}
{{ myArray[keyTwoStr] }}

On the third line of the above code, the string key '2' is actually cast to integer 2, like PHP's documentation of arrays states:

Strings containing valid decimal integers, unless the number is preceded by a + sign, will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.

If for some reason you need an integer, you can cast a string to an int by performing math operations on it – e.g. variable * 1 (= "32" * 1) or variable + 0 (= "32" + 0).

Interweave answered 8/4, 2016 at 14:15 Comment(1)
Adding + 0 was the only solution that converted the string to an integer. Thanks.Liberal
J
2

None of the previous solution is good if you want to get just a intval. format_number add decimal to string. This solution is maybe the worst.

What worked for me is

Add a custom twig function filter just like :

public function getFilters(): array
{
    return [
        new TwigFilter('force_to_int', fn ($value) => intval($value))
    ];
}

Use it like :

{{ str_to_convert|force_to_int }}
Jer answered 20/5, 2021 at 14:42 Comment(0)
K
0

Here's my take based on this answer.

{{ 0 + myNumber }} // returns int(3)
Knickerbockers answered 16/5, 2023 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.