Skip to content

Twig 3 Certification Test 2

Instructions

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

Questions

Basic Concepts

  1. What is the main purpose of Twig?

    • A) To manage databases
    • B) To create web applications
    • C) To serve as a template engine for PHP
    • D) To handle HTTP requests
  2. Which of the following is a valid Twig syntax for a variable?

    • A) {{ variable }}
    • B) {% variable %}
    • C) << variable >>
    • D) [variable]
  3. What is the purpose of the |escape filter?

    • A) To remove whitespace
    • B) To convert a string to lowercase
    • C) To escape HTML characters
    • D) To format a date

Filters

  1. Which filter would you use to capitalize the first letter of a string?

    • A) capitalize
    • B) upper
    • C) title
    • D) first
  2. What does the |length filter do?

    • A) Returns the length of a string
    • B) Returns the number of elements in an array
    • C) Both A and B
    • D) None of the above
  3. Which of the following is a valid way to chain filters in Twig?

    • A) {{ variable|filter1|filter2 }}
    • B) {{ filter1(filter2(variable)) }}
    • C) {{ variable|filter1(filter2) }}
    • D) {{ filter1|filter2(variable) }}

Tests

  1. Which test checks if a variable is empty?

    • A) empty
    • B) null
    • C) defined
    • D) exists
  2. How do you combine tests in Twig?

    • A) Using and and or
    • B) Using && and ||
    • C) Using & and |
    • D) All of the above

Control Structures

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

    • A) {% while condition %}
    • B) {% for condition %}
    • C) {% loop condition %}
    • D) Twig does not support while loops.
  2. How do you create a nested loop in Twig?

    • A) {% for item in items %}{% for subitem in subitems %}{% endfor %}{% endfor %}
    • B) {% loop item in items %}{% loop subitem in subitems %}{% endloop %}{% endloop %}
    • C) {% each item in items %}{% each subitem in subitems %}{% endeach %}{% endeach %}
    • D) None of the above.

Variables and Expressions

  1. What is the output of {{ variable is defined ? 'Yes' : 'No' }} if variable is not defined?

    • A) Yes
    • B) No
    • C) Error
    • D) null
  2. How do you access a property of an object in Twig?

    • A) {{ object.property }}
    • B) {{ object->property }}
    • C) {{ object[property] }}
    • D) Both A and C

Functions

  1. Which function is used to get the current date in Twig?

    • A) current_date()
    • B) date()
    • C) now()
    • D) today()
  2. What is the purpose of the include function in Twig?

    • A) To include a file
    • B) To include a template
    • C) To include a variable
    • D) To include a string

Operators

  1. What does the in operator do in Twig?

    • A) Checks if a value exists in an array
    • B) Checks if a variable is defined
    • C) Checks if a string contains a substring
    • D) None of the above
  2. Which of the following is a valid operator for string comparison in Twig?

    • A) =
    • B) ==
    • C) ===
    • D) Both B and C

Template Re-use

  1. What is the purpose of the block tag in Twig?

    • A) To define a variable
    • B) To create a reusable template section
    • C) To include a template
    • D) To extend a template
  2. How do you include a template fragment in Twig?

    • A) {% include 'fragment.twig' %}
    • B) {% embed 'fragment.twig' %}
    • C) {% import 'fragment.twig' %}
    • D) {% extend 'fragment.twig' %}

Mixed Questions

  1. What is the output of {{ 'Hello'|upper }}?

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

    • A) {% if variable is not null %}
    • B) {% if variable != null %}
    • C) {% if variable is defined and variable is not null %}
    • D) All of the above
  3. Which of the following is a valid way to define a macro in Twig?

    • A) {% macro name() %}
    • B) {% define macro name() %}
    • C) {% function name() %}
    • D) {% template name() %}
  4. What does the |date filter do?

    • A) Formats a date string
    • B) Converts a string to a date
    • C) Returns the current date
    • D) None of the above
  5. 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) Both A and B
    • D) None of the above
  6. 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) To join two hashes
  7. What does the |slice filter do?

    • A) Extracts a substring
    • B) Returns a portion of an array
    • C) Both A and B
    • D) None of the above

Advanced Questions

  1. How do you check if an array contains a specific value in Twig?

    • A) value in array
    • B) array.contains(value)
    • C) array.includes(value)
    • D) array.has(value)
  2. What is the output of {{ [1, 2, 3]|length }}?

    • A) 3
    • B) 2
    • C) 1
    • D) Error
  3. Which of the following is a valid way to create a nested block in Twig?

    • A) {% block outer %}{% block inner %}{% endblock %}{% endblock %}
    • B) {% block outer %}{% endblock inner %}
    • C) {% block outer %}{% block inner %}{% endblock inner %}{% endblock outer %}
    • D) {% block outer %}{% block inner %}{% endblock outer %}{% endblock inner %}
  4. How do you create a custom filter in Twig?

    • A) {% filter name %}
    • B) {% define filter name %}
    • C) {% filter name(value) %}
    • D) {% filter(name) %}
  5. What is the purpose of the |default filter?

    • A) To provide a default value if a variable is undefined
    • B) To format a date
    • C) To convert a string to lowercase
    • D) To escape HTML
  6. What is the output of {{ 'Twig'|length }}?

    • A) 4
    • B) 3
    • C) 5
    • D) Error
  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 does the |merge filter do?

    • A) Merges two arrays
    • B) Merges two hashes
    • C) Merges two strings
    • D) Both A and B
  9. What is the purpose of the |reverse filter?

    • A) To reverse a string
    • B) To reverse an array
    • C) To reverse a hash
    • D) All of the above
  10. 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 %}
  11. What is the output of {{ 'Hello World'|slice(0, 5) }}?

    • A) Hello
    • B) Hello World
    • C) World
    • D) Error
  12. How do you create a conditional statement that checks if a variable is an array?

    • A) {% if variable is iterable %}
    • B) {% if variable is array %}
    • C) {% if variable is defined and variable is not null %}
    • D) All of the above
  13. What is the purpose of the |sort filter?

    • A) To sort an array in ascending order
    • B) To sort a hash by keys
    • C) To sort a hash by values
    • D) All of the above
  14. How do you create a loop that iterates over an array in reverse order?

    • A) {% for item in array|reverse %}
    • B) {% for item in array %}{% endfor %}
    • C) {% for item in reverse(array) %}
    • D) {% for item in array|sort('desc') %}
  15. What is the purpose of the |batch filter?

    • 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
  16. 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 %}
  17. What is the output of {{ 10 % 3 }}?

    • A) 3
    • B) 1
    • C) 0
    • D) Error
  18. What is the purpose of the |first filter?

    • A) To get the first element of an array
    • B) To get the first character of a string
    • C) To get the first key of a hash
    • D) All of the above
  19. How do you create a loop that iterates over an array and skips every second element?

    • A) {% for item in array|slice(2) %}
    • B) {% for item in array|batch(2) %}
    • C) {% for item in array|slice(0, 2) %}
    • D) {% for item in array|slice(1, 2) %}
  20. What does the |join filter do?

    • A) Joins two strings
    • B) Joins an array into a string
    • C) Joins two arrays
    • D) Joins two hashes

Answers

Basic Concepts

  1. C) To serve as a template engine for PHP
  2. A) {{ variable }}
  3. C) To escape HTML characters

Filters

  1. A) capitalize
  2. C) Both A and B
  3. A) {{ variable|filter1|filter2 }}

Tests

  1. A) empty
  2. A) Using and and or

Control Structures

  1. D) Twig does not support while loops.
  2. A) {% for item in items %}{% for subitem in subitems %}{% endfor %}{% endfor %}

Variables and Expressions

  1. B) No
  2. A) {{ object.property }}

Functions

  1. C) now()
  2. B) To include a template

Operators

  1. A) Checks if a value exists in an array
  2. D) Both B and C

Template Re-use

  1. B) To create a reusable template section
  2. A) {% include 'fragment.twig' %}

Mixed Questions

  1. C) HELLO
  2. D) All of the above
  3. A) {% macro name() %}
  4. A) Formats a date string
  5. C) Both A and B
  6. B) To join an array into a string
  7. C) Both A and B

Advanced Questions

  1. A) value in array
  2. A) 3
  3. A) {% block outer %}{% block inner %}{% endblock %}{% endblock %}
  4. A) {% filter name %}
  5. A) To provide a default value if a variable is undefined
  6. A) 4
  7. A) {% for key, value in hash %}
  8. D) Both A and B
  9. B) To reverse an array
  10. A) {% for key, value in hash %}

  11. A) Hello

  12. A) {% if variable is iterable %}
  13. D) All of the above
  14. A) {% for item in array|reverse %}
  15. A) To batch an array into smaller arrays
  16. A) {% for key, value in hash %}
  17. B) 1
  18. A) To get the first element of an array
  19. B) {% for item in array|batch(2) %}
  20. B) Joins an array into a string

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