Custom rendering of a “repeated” field from Symfony 2 in Twig

After a random guess I solved my own problem. I’ll post it here so others who might come to this question by searching also know the answer: {% for passwordField in form.password %} <div class=”form-field”> {{ form_label(passwordField, null, { ‘attr’: {‘class’: ‘form-label’} }) }} {{ form_widget(passwordField, { ‘attr’: {‘class’: ‘form-input’} }) }} </div> {% endfor … Read more

A count in a loop

Apparently twig defines some loop variables inside the for-loop: {% for v in a %} {% if loop.index0 is even %} … {% endif %} {% endfor %}

How to remove duplicated items in array on Twig

Twig is a VIEW engine, and should not be used – in theory – to manipulate data. It’s a (very) good practice to use (assumingly) PHP to gather data, do all necessary manipulations and then pass the right data to your view. That said, here’s how you can do it in pure Twig syntax: {% … Read more

Using an environment variable (from `.env` file) in custom Twig function in Symfony 4

Here’s an easier way (Symfony 4) that does not involve any custom extensions. In my case, I wanted to set the Google Tag Manager Id as an environment variable in the .env file: GOOGLE_TAG_MANAGER_ID=”GTM-AAA12XX” Next, reference the environment variable in the config/packages/twig.yaml file: twig: globals: google_tag_manager_id: ‘%env(GOOGLE_TAG_MANAGER_ID)%’ Now you can use the tag manager value … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)