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
-
What is the default file extension for Twig templates?
- A) .twig
- B) .tpl
- C) .html
- D) .php
-
What syntax is used to display a variable in Twig?
- A)
{% variable %}
- B)
{{ variable }}
- C)
[[ variable ]]
- D)
<< variable >>
- A)
-
How do you escape output in Twig?
- A)
{{ variable|escape }}
- B)
{{ escape(variable) }}
- C)
{% escape variable %}
- D)
{{ variable|safe }}
- A)
Filters
-
Which of the following is NOT a valid Twig filter?
- A)
trim
- B)
replace
- C)
capitalize
- D)
substring
- A)
-
What does the
|raw
filter do in Twig?- A) Escapes HTML
- B) Outputs unescaped HTML
- C) Converts to string
- D) Formats dates
-
Which filter would you use to convert a string to lowercase in Twig?
- A)
lower
- B)
lowercase
- C)
toLower
- D)
downcase
- A)
Tests
-
Which of the following is a valid Twig test?
- A)
even
- B)
odd
- C)
divisibleby
- D) All of the above
- A)
-
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 %}
- A)
Control Structures
-
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 %}
- A)
-
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
- A)
Variables and Expressions
-
How do you define a variable in Twig?
- A)
{% set variable = value %}
- B)
{{ variable = value }}
- C)
<< variable = value >>
- D)
[set variable = value]
- A)
-
What is the output of
{{ 5 + 5 }}
?- A)
10
- B)
55
- C)
5 + 5
- D)
Error
- A)
Functions
-
Which of the following is a valid Twig function?
- A)
date()
- B)
length()
- C)
json_encode()
- D) All of the above
- A)
-
What function would you use to include a template and pass variables to it?
- A)
include()
- B)
render()
- C)
include_with()
- D)
render_with()
- A)
Operators
-
What operator is used to concatenate strings in Twig?
- A)
+
- B)
.
- C)
&
- D)
,
- A)
-
Which of the following is a valid Twig operator?
- A)
==
- B)
===
- C)
!=
- D) All of the above
- A)
Template Re-use
-
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' %}
- A)
-
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
-
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
- A)
-
How do you create a Twig environment?
- A)
new Twig_Environment()
- B)
new Twig\Environment()
- C)
Twig::createEnvironment()
- D)
Twig\Environment::new()
- A)
-
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
-
How do you define a macro in Twig?
- A)
{% macro name() %}
- B)
{% define macro name() %}
- C)
{% function name() %}
- D)
{% template name() %}
- A)
-
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
- A)
-
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
- A)
-
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
-
How do you create a custom filter in Twig?
- A)
{% filter name %}
- B)
{% define filter name %}
- C)
{% filter name(value) %}
- D)
{% filter(name) %}
- A)
-
What is the output of
{{ "Hello"|upper }}
?- A)
Hello
- B)
hello
- C)
HELLO
- D)
hElLo
- A)
-
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 %}
- A)
-
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
-
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 %}
- A)
-
What is the output of
{{ 5 / 2 }}
?- A)
2
- B)
2.5
- C)
2.0
- D)
2.50
- A)
-
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 %}
- A)
-
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
-
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 %}
- A)
-
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)
-
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 %}
- A)
-
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
-
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 %}
- A)
-
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
-
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 '.' %}
- A)
-
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
-
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' %}
- A)
-
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
-
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' %}
- A)
-
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
- A) .twig
- B)
{{ variable }}
- A)
{{ variable|escape }}
Filters
- D)
substring
- B) Outputs unescaped HTML
- A)
lower
Tests
- D) All of the above
- A)
{% if variable is defined %}
Control Structures
- A)
{% for item in items %}
- D) Both B and C
Variables and Expressions
- A)
{% set variable = value %}
- B)
10
Functions
- D) All of the above
- A)
include()
Operators
- B)
.
- D) All of the above
Template Re-use
- A)
{% extends 'parent.twig' %}
- A) To include a template fragment
Mixed Questions
- A)
1, 2, 3
- B)
new Twig\Environment()
- B) To pass variables to included templates
- A)
{% macro name() %}
- A)
{# This is a comment #}
- D) All of the above
- A) To extract a substring
Advanced Questions
- A)
{% filter name %}
- C)
HELLO
- A)
{% if variable is empty %}
- B) To join an array into a string
- A)
{% block outer %}{% block inner %}{% endblock %}{% endblock %}
- B)
2.5
- A)
{% for key, value in hash %}
- A) To provide a default value if a variable is undefined or null
- A)
{% if condition1 %}{% if condition2 %}{% endif %}{% endif %}
- A) To format a string with placeholders
Advanced Questions (continued)
- A)
{% for key, value in hash %}
- A) To merge two arrays
- A)
{% for key, value in hash %}
- A) To sort an array in ascending order
- D)
{% for key, value in hash with keys using separator '.' %}
- B) To reverse an array
- D)
{% for key, value in hash with keys using separator '.' and format 'key_%s', value using format 'value_%s' %}
- A) To batch an array into smaller arrays
- D)
{% for key, value in hash with keys using separator '.' and format 'key_%s', value using format 'value_%s' %}
- 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!