Read and answer or little quiz of 20 questions that every Twig developer should be able to solve.

20 Questions that every Twig Developer Should be Able to Answer

In this post, we want to share with you 20 questions that every Twig developer should be able to answer. You can think of this as a quiz and you can obviously evaluate your knowledge with Twig. Before starting, please keep in mind that:

  • Some questions have multiple answers.
  • The questions are Twig related only, nothing about frameworks that implement it like Symfony.
  • You aprove the test with 14 (70%) correct answered questions.

Once you have made the test, you can find the answers here. Having said that, let's get started !

Note

To make an honest quiz, try to do the quiz without searching for a solution. Remember that if you don't know the answer of a couple of them it doesn't mean that you are bad with Twig ! It just means that there are things that you didn't even knew, so be sure to keep learning Twig with the official documentation.

1. How can you declare a variable in Twig?

  1. {% define myVariable = 'My Text' %}
  2. {% set myVariable %}My Text{% endset %}
  3. {% set myVariable = 'My Text' %}
  4. {% set myVariable %}{{ 'My Text' }}{% endset %}
  5. {% set myVariable = 'My Text', 'Other Text' %}

2. What's the output of the following snippet?

{{ {name: "Xavier", lastName: "Nemo", first: false, address: "P Sherman, 42 Wallaby Way, Sydney"} | first }}
  1. The filter “first” does not exist at Twig
  2. Error: Array to string conversion
  3. name
  4. Xavier
  5. False

3. What is the comment syntax for Twig?

  1. {{- Comment Text -}}
  2. {[- Comment Text -]}
  3. {{# Comment Text #}}
  4. {# Comment Text #}
  5. {[ Comment Text ]}

4. The Output Escaping in Twig

  1. is enabled by default.
  2. can be disabled specifically in some variable with the raw filter.
  3. is disabled by default.
  4. is necessary to prevent XSS attacks.
  5. Twig is not able to escape the output.

5. Does Twig create a special variable in the scope of a for loop to get information about the current iteration? If it does, what is its name?

  1. Twig doesn't offer such a feature.
  2. cycle
  3. loop
  4. forLoop
  5. iteration

6. What would be the output of the following snippet?

{% verbatim %}
    <ul>
    {% for row in rows %}
        <li title="{{ rows.title }}">{{ rows.text }}</li>
    {% endfor %}
    </ul>
{% endverbatim %}

A.

<ul>
    <li title="Row 1 Title">Row 1 Text</li>
    <li title="Row 2 Title">Row 2 Text</li>
    <li title="Row 3 Title">Row 3 Text</li>
    <li title="Row 4 Title">Row 4 Text</li>
</ul>

B.

<ul>
    {% for row in rows %}
        <li title="{{ rows.title }}">{{ rows.text }}</li>
    {% endfor %}
</ul>

C. The block type verbatim does not exist.

D. Variable rows does not exist.

7. Which of the following default filters doesn't exist in Twig?

  1. unescape
  2. join
  3. length
  4. split
  5. strtolower

8. What is the output of the following snippet?

{% spaceless %}
    <div   class="welcome-alert">
        <h1 >Welcome David !</h1>
    </div>
{% endspaceless %}
  1. <div class="welcome-alert"><h1>Welcome David!</h1></div>
  2. Template Error: Spaceless block expects HTML content to be properly idented.
  3. <div class="welcome-alert"><h1>Welcome David !</h1></div>
  4. <div   class="welcome-alert"><h1 >Welcome David !</h1></div>
  5. <divclass="welcome-alert"><h1>WelcomeDavid!</h1></div>

9. What is the correct syntax to concatenate strings?

  1. {{ "First Name" + " " + "Last Name" }}
  2. {{ "First Name" ^ "Last Name" }}
  3. {{ "First Name" ~ " " ~ "Last Name" }}
  4. {{ "First Name" "Last Name" }}
  5. {{ "First Name". " " ."Last Name" }}

10. How can you verify if 2 variables are identical (important identical not equal)?

  1. {% if variableA == variableB %}
  2. {% if variableA === variableB %}
  3. {% if variableA >< variableB %}
  4. {% if variableA is identical to variableB %}
  5. {% if variableA is same as(variableB) %}

11. It's possible to use the same PHP constants in Twig by default?

  1. No, you would need to create a custom extension.
  2. Yes, wrapping the constant name with triple brackets and single quotes.
  3. Yes, using the constant function.
  4. Yes, using the get_constant_value filter.
  5. No, it is not possible.

12. How can you print a random value from an array with Twig?

  1. {{ random([1,2,3]) }}
  2. {{ [1,2,3]|random}}
  3. {{ randomize() }}
  4. {{ [1,2,3]|randomize}}
  5. It is not possible.

13. How can you print the value of the "a-b" attribute from the following key-value array?

{% set MyVariable = {
    "a": 1,
    "b": 2,
    "c": 3,
    "a-b": 4
}%}
  1. {{ MyVariable("a-b") }}
  2. {{ MyVariable["a-b"]}}
  3. {{ attribute(MyVariable, 'a-b') }}
  4. {{ MyVariable.a-b }}
  5. {{ MyVariable.get('a-b')}}

14. What is the output of the following snippet?

{% with %}
    {% set MyVariable = 42 %}
{% endwith %}

{{ MyVariable }}
  1. A Twig's runtime exception has been trown. Variable "MyVariable" does not exist.
  2. 42
  3. 82
  4. Null
  5. The given Twig code contains syntax error(s). Unknown "with" tag. 

15. Which of the following tests keywords do not exist in Twig?

  1. {% if a is divisible by(b) %}
  2. {% if a is iterable %}
  3. {% if a is empty %}
  4. {% if a is odd %}
  5. {% if a is instanceof b %}

16. It is possible to print the content of an already existing block twice?

  1. Yes, appending the copy keyword before the next block e.g {% copy BlockName %}.
  2. Yes, using the block method with the name of the block you want to copy as argument e.g {{ block("BlockName")}}
  3. Yes, by simply declaring the block twice.
  4. Yes, using the clone keyword and a printing statement with the name of the block e.g {{ clone BlockName }}
  5. It is not possible.

17. What is the output of the following snippet?

{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', "h"] %}

<table>
{% for row in items|batch(4) %}
    <tr>
        {%- for column in row -%}
            <td>{{ column }}</td>
        {%- endfor -%}
    </tr>
{% endfor %}
</table>

A.

<table>
    <tr><td>a</td><td>b</td><td>c</td><td>d</td></tr>
    <tr><td>e</td><td>f</td><td>g</td><td>h</td></tr>
</table>

B.

<table><tr><td>a</td><td>b</td><td>c</td><td>d</td></tr><tr><td>e</td><td>f</td><td>g</td><td>h</td></tr></table>

C. The filter “batch” does not exist at Twig.

D. The Batch filter expects a string as first argument, integer given.

18. What is the output of the following snippet?

{{ 12.4 // 2 }}
  1. The given Twig code contains syntax error(s). Unexpected character "/".
  2. 6.2
  3. 6
  4. 3.1
  5. Notice: Array to string conversion.

19. Which of the following snippets allows to print the Alphabet separated by a comma?

A.

{%- for i in range("a", "z") -%}
    {{ i }},
{%- endfor -%}

B.

{%- for i in (from "a" to "z") -%}
    {{ i }},
{%- endfor -%}

C.

{%- for i in range(a, z) -%}
    {{ i }},
{%- endfor -%}

D.

{%- for i in "a".."z" -%}
    {{ i }},
{%- endfor -%}

E.

{%- for i in "a" to "z" -%}
    {{ i }},
{%- endfor -%}

20. What is the output of the following snippet?

{{ app.username|default('User is not logged in') }}
  1. User is not logged in
  2. Variable "app" does not exist.
  3. Key "username" for array does not exist.
  4. The filter “default” does not exist at Twig
  5. Key "User is not logged in" for array does not exist.

Congratulations, you have made it to the end of the test! If you think that you know the answer to all of them, we invite you to compare your results with the correct answers and explanation to them here.

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors