Skip to content

Twig 3 Certification Test

Instructions

This test consists of 45 questions about Twig 3. It covers 8 topics and has a time limit of 60 minutes. Choose the best answer for each question.

Questions

Basic Concepts

  1. What is the default file extension for Twig templates?

    • A) .twig
    • B) .tpl
    • C) .html
    • D) .php
  2. What syntax is used to display a variable in Twig?

    • A) {% variable %}
    • B) {{ variable }}
    • C) [[ variable ]]
    • D) << variable >>
  3. How do you escape output in Twig?

    • A) {{ variable|escape }}
    • B) {{ escape(variable) }}
    • C) {% escape variable %}
    • D) {{ variable|safe }}

Filters

  1. Which of the following is NOT a valid Twig filter?

    • A) trim
    • B) replace
    • C) capitalize
    • D) substring
  2. What does the |raw filter do in Twig?

    • A) Escapes HTML
    • B) Outputs unescaped HTML
    • C) Converts to string
    • D) Formats dates
  3. Which filter would you use to convert a string to lowercase in Twig?

    • A) lower
    • B) lowercase
    • C) toLower
    • D) downcase

Tests

  1. Which of the following is a valid Twig test?

    • A) even
    • B) odd
    • C) divisibleby
    • D) All of the above
  2. How do you check if a variable is defined using a test in Twig?

    • A) {% if variable is defined %}
    • B) {% if defined(variable) %}
    • C) {% if variable exists %}
    • D) {% if variable is set %}

Control Structures

  1. What is the correct syntax for a Twig for loop?

    • A) {% for item in items %}
    • B) {% loop item in items %}
    • C) {% each item in items %}
    • D) {% iterate items %}
  2. How do you create an if-else statement in Twig?

    • A) {% if condition %}{% else %}{% endif %}
    • B) {% if condition %}{% elseif condition %}{% else %}{% endif %}
    • C) {% if condition %}{% elif condition %}{% else %}{% endif %}
    • D) Both B and C

Variables and Expressions

  1. How do you define a variable in Twig?

    • A) {% set variable = value %}
    • B) {{ variable = value }}
    • C) << variable = value >>
    • D) [set variable = value]
  2. What is the output of {{ 5 + 5 }}?

    • A) 10
    • B) 55
    • C) 5 + 5
    • D) Error

Functions

  1. Which of the following is a valid Twig function?

    • A) date()
    • B) length()
    • C) json_encode()
    • D) All of the above
  2. What function would you use to include a template and pass variables to it?

    • A) include()
    • B) render()
    • C) include_with()
    • D) render_with()

Operators

  1. What operator is used to concatenate strings in Twig?

    • A) +
    • B) .
    • C) &
    • D) ,
  2. Which of the following is a valid Twig operator?

    • A) ==
    • B) ===
    • C) !=
    • D) All of the above

Template Re-use

  1. How do you extend a parent template in Twig?

    • A) {% extends 'parent.twig' %}
    • B) {% inherit 'parent.twig' %}
    • C) {% include 'parent.twig' %}
    • D) {% use 'parent.twig' %}
  2. What is the purpose of the include tag in Twig?

    • A) To include a template fragment
    • B) To embed a template
    • C) To extend a parent template
    • D) To define a macro

Mixed Questions

  1. What is the output of {{ [1, 2, 3]|join(', ') }}?

    • A) 1, 2, 3
    • B) [1, 2, 3]
    • C) "1, 2, 3"
    • D) 1 2 3
  2. How do you create a Twig environment?

    • A) new Twig_Environment()
    • B) new Twig\Environment()
    • C) Twig::createEnvironment()
    • D) Twig\Environment::new()
  3. What is the purpose of the with keyword in Twig?

    • A) To define a variable
    • B) To pass variables to included templates
    • C) To create a loop
    • D) To filter output
  4. How do you define a macro in Twig?

    • A) {% macro name() %}
    • B) {% define macro name() %}
    • C) {% function name() %}
    • D) {% template name() %}
  5. What is the correct syntax for a Twig comment?

    • A) {# This is a comment #}
    • B) <!-- This is a comment -->
    • C) {% comment %} This is a comment {% endcomment %}
    • D) // This is a comment
  6. How do you create a loop that iterates over a range of numbers in Twig?

    • A) {% for i in 1..10 %}
    • B) {% for i from 1 to 10 %}
    • C) {% for i in range(1, 10) %}
    • D) All of the above
  7. What is the purpose of the |slice filter in Twig?

    • A) To extract a substring
    • B) To format a string
    • C) To split a string
    • D) To remove whitespace

Advanced Questions

  1. How do you create a custom filter in Twig?

    • A) {% filter name %}
    • B) {% define filter name %}
    • C) {% filter name(value) %}
    • D) {% filter(name) %}
  2. What is the output of {{ "Hello"|upper }}?

    • A) Hello
    • B) hello
    • C) HELLO
    • D) hElLo
  3. How do you create a conditional statement that checks if a variable is empty?

    • A) {% if variable is empty %}
    • B) {% if empty(variable) %}
    • C) {% if variable == null %}
    • D) {% if variable is defined and variable is not null %}
  4. What is the purpose of the |join filter in Twig?

    • A) To join two strings
    • B) To join an array into a string
    • C) To join two arrays
    • D) None of the above
  5. How do you create a nested block in Twig?

    • A) {% block outer %}{% block inner %}{% endblock %}{% endblock %}
    • B) {% block outer %}{% block inner %}
    • C) {% block outer %}{% endblock %}{% block inner %}{% endblock %}
    • D) {% block outer %}{% endblock inner %}
  6. What is the output of {{ 5 / 2 }}?

    • A) 2
    • B) 2.5
    • C) 2.0
    • D) 2.50
  7. How do you create a loop that iterates over a hash in Twig?

    • A) {% for key, value in hash %}
    • B) {% for value in hash %}
    • C) {% for key => value in hash %}
    • D) {% for item in hash %}
  8. What is the purpose of the |default filter in Twig?

    • A) To provide a default value if a variable is undefined or null
    • B) To format a date
    • C) To convert a string to lowercase
    • D) To escape HTML
  9. How do you create a nested if statement in Twig?

    • A) {% if condition1 %}{% if condition2 %}{% endif %}{% endif %}
    • B) {% if condition1 and condition2 %}
    • C) {% if condition1 or condition2 %}
    • D) {% if condition1 %}{% elseif condition2 %}{% endif %}
  10. What is the purpose of the |format filter in Twig?

    • A) To format a string with placeholders
    • B) To format a date
    • C) To format a number
    • D) To format a boolean

Advanced Questions (continued)

  1. How do you create a loop that iterates over a hash and preserves the keys?

    • A) {% for key, value in hash %}
    • B) {% for value in hash %}
    • C) {% for key => value in hash %}
    • D) {% for item, value in hash %}
  2. What is the purpose of the |merge filter in Twig?

    • A) To merge two arrays
    • B) To merge two hashes
    • C) To merge two strings
    • D) To merge two variables
  3. How do you create a loop that iterates over a hash and preserves the keys as variables?

    • A) {% for key, value in hash %}
    • B) {% for value in hash %}
    • C) {% for key => value in hash %}
    • D) {% for key, value in hash with keys %}
  4. What is the purpose of the |sort filter in Twig?

    • A) To sort an array in ascending order
    • B) To sort an array in descending order
    • C) To sort a hash by keys
    • D) To sort a hash by values
  5. How do you create a loop that iterates over a hash and preserves the keys as variables with a custom separator?

    • A) {% for key, value in hash %}
    • B) {% for value in hash %}
    • C) {% for key => value in hash %}
    • D) {% for key, value in hash with keys using separator '.' %}
  6. What is the purpose of the |reverse filter in Twig?

    • A) To reverse a string
    • B) To reverse an array
    • C) To reverse a hash
    • D) To reverse a number
  7. How do you create a loop that iterates over a hash and preserves the keys as variables with a custom separator and a custom key format?

    • A) {% for key, value in hash %}
    • B) {% for value in hash %}
    • C) {% for key => value in hash %}
    • D) {% for key, value in hash with keys using separator '.' and format 'key_%s' %}
  8. What is the purpose of the |batch filter in Twig?

    • A) To batch an array into smaller arrays
    • B) To batch a hash into smaller hashes
    • C) To batch a string into smaller strings
    • D) To batch a number into smaller numbers
  9. How do you create a loop that iterates over a hash and preserves the keys as variables with a custom separator, a custom key format, and a custom value format?

    • A) {% for key, value in hash %}
    • B) {% for value in hash %}
    • C) {% for key => value in hash %}
    • D) {% for key, value in hash with keys using separator '.' and format 'key_%s', value using format 'value_%s' %}
  10. What is the purpose of the |map filter in Twig?

    • A) To map an array to a new array based on a callback function
    • B) To map a hash to a new hash based on a callback function
    • C) To map a string to a new string based on a callback function
    • D) To map a number to a new number based on a callback function

Twig 3 Certification Test Answers

Here are the correct answers for the Twig 3 Certification Test questions:

Answers

Basic Concepts

  1. A) .twig
  2. B) {{ variable }}
  3. A) {{ variable|escape }}

Filters

  1. D) substring
  2. B) Outputs unescaped HTML
  3. A) lower

Tests

  1. D) All of the above
  2. A) {% if variable is defined %}

Control Structures

  1. A) {% for item in items %}
  2. D) Both B and C

Variables and Expressions

  1. A) {% set variable = value %}
  2. B) 10

Functions

  1. D) All of the above
  2. A) include()

Operators

  1. B) .
  2. D) All of the above

Template Re-use

  1. A) {% extends 'parent.twig' %}
  2. A) To include a template fragment

Mixed Questions

  1. A) 1, 2, 3
  2. B) new Twig\Environment()
  3. B) To pass variables to included templates
  4. A) {% macro name() %}
  5. A) {# This is a comment #}
  6. D) All of the above
  7. A) To extract a substring

Advanced Questions

  1. A) {% filter name %}
  2. C) HELLO
  3. A) {% if variable is empty %}
  4. B) To join an array into a string
  5. A) {% block outer %}{% block inner %}{% endblock %}{% endblock %}
  6. B) 2.5
  7. A) {% for key, value in hash %}
  8. A) To provide a default value if a variable is undefined or null
  9. A) {% if condition1 %}{% if condition2 %}{% endif %}{% endif %}
  10. A) To format a string with placeholders

Advanced Questions (continued)

  1. A) {% for key, value in hash %}
  2. A) To merge two arrays
  3. A) {% for key, value in hash %}
  4. A) To sort an array in ascending order
  5. D) {% for key, value in hash with keys using separator '.' %}
  6. B) To reverse an array
  7. D) {% for key, value in hash with keys using separator '.' and format 'key_%s', value using format 'value_%s' %}
  8. A) To batch an array into smaller arrays
  9. D) {% for key, value in hash with keys using separator '.' and format 'key_%s', value using format 'value_%s' %}
  10. A) To map an array to a new array based on a callback function

Feel free to review your answers and see how well you understand Twig 3!