Viewed   271 times

I read in the Twig documentation that it is possible to iterate over an associative array in the following manner:

{% for key, value in array %}  
 {{key}}  
 {{value}}  
{% endfor %}  

I was wondering whether this is possible for objects of type stdClass as well.

I would have expected Twig to iterate over the property values of the object taking the property names as keys. Instead, the instruction block contained in the for loop is not executed at all.

 Answers

5

You can first cast the object to array. You can build own filter casting your object to array. More about filters is available here: http://twig.sensiolabs.org/doc/advanced.html#filters

It could then look like that:

{% for key, value in my_object|cast_to_array %}
Monday, August 29, 2022
2
<select name="country"data-width="100%">
    {% for key,val in form.country.vars.choices %}
        <option value="{{ val.value }}" {{  form.country.vars.value == '' and key == 0 ? ' selected ' :(val.value == form.country.vars.value ? ' selected ' : '') }}>{{ val.label | trans }}</option>
    {% endfor %}
</select>
Tuesday, August 9, 2022
5

For the first line, use set to define a variable:

{% if language.code == 'bg' %}
    {% set cur_ = 'BGN' %}
{% else %}
    {% set cur_ = 'USD' %}
{% endif %}

Better yet, use the ternary operator:

{% set cur_ = (language.code == 'bg') ? 'BGN' : 'USD' %}

For the second, just replace <?php echo $...; ?> by {{...}}:

onclick="$('input[name='code']').attr('value', '{{ language.code }}');$('input[name='currency_code']').attr('value', '{{ cur_ }}');  $(this).parent().parent().submit();"onclick="$('input[name='code']').attr('value', '{{ language.code }}');$('input[name='currency_code']').attr('value', '{{ cur_ }}');  $(this).parent().parent().submit();"
Thursday, August 11, 2022
 
2
foreach(var prop in myVar.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
   Console.WriteLine("Name: {0}, Value: {1}",prop.Name, prop.GetValue(myVar,null));
}
Monday, November 28, 2022
5

Install the PHP intl extension

First of all, you will need the PHP intl extension, as the Twig extension is built on top of that. The Twig Intl extension will throw an Exception if the PHP intl extension is not enabled. Installation instructions can be found in the official PHP documentation.

On Ubuntu/Debian machines, this is as easy as running the following command:

sudo apt install php-intl

On Windows machines, you probably have to uncomment the following line in php.ini:

extension=php_intl.dll

For CentOS, or other architectures, follow the instructions here. Note that CentOS requires both PECL and the GCC C++ compiler to be installed: yum install php-pear and yum install gcc-c++.

Once the extension is added to php.ini, then restart the web server.

Install the Twig Extensions

Next, you will need the Twig Extensions package (that contains the Intl extension, among others), which can be installed using Composer. Run this command in the command line:

composer require twig/extensions

This will add the dependency to your composer.json and download it.

Note: the localizednumber and localizedcurrency filters were introduced in version 1.2.0, so you need at least that version if you want to use them.

Adding the extension to Twig

If you are using Twig directly (i.e. not in a Symfony project), add the extension to the Twig environment manually:

<?php

use TwigEnvironment;
use TwigExtensionsIntlExtension;

$twig = new Environment($loader);
$twig->addExtension(new IntlExtension());

Adding the extension to Twig (in Symfony)

If you are using a Symfony application, you can add the extension to Twig by creating a service and tagging it as a Twig extension in config/services.yml:

services:
    twig.extension.intl:
        class: TwigExtensionsIntlExtension
        tags:
            - { name: twig.extension }

Setting the default locale

<?php

Locale::setDefault('nl-NL');

Setting the default locale in Symfony

In config/framework.yaml, uncomment the default_locale setting:

framework:
    default_locale: en
Saturday, November 5, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :