Home » PHP » Twig » Top Twig Interview Questions And Answers Part 6

Top Twig Interview Questions And Answers Part 6

1. How can we check form field has errors in Twig Template?

{% if form.vars.errors or form.fieldName.vars.errors %}

{% endif %}

2. How to concatnate strings in Twig Template?

{{ ('http://' ~ app.request.host) | trans }}

OR

{% set A = "Hello" %}
{%set B = "World" %}
{{ A ~ B }}

3. How to access class constants in Twig Template?


class Person {
const TYPE_AGE = 24;
}
{% if var == constant('Namespace\\Person::TYPE_AGE') %}
{# or #}
{% if var is constant('Namespace\\Person::TYPE_AGE') %}

4. How to check for Null value in Twig Template?

{% if var is null %}
    {# do something #}
{% endif %}

5. How to check for variable is not defined in Twig Template?

{% if var is not defined %}
    {# do something #}
{% endif %}

Check Also

Top Twig Template Interview And Answer Part 3

1. How the ternary operator work in twig template? {{ admin ? 'Admin' : 'User' …

Leave a Reply

Your email address will not be published. Required fields are marked *