Twig 3 Certification Test 3
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
-
What is the main advantage of using Twig over plain PHP for templating?
- A) Twig is faster than PHP
- B) Twig provides a cleaner syntax and better separation of logic and presentation
- C) Twig supports more programming languages
- D) Twig is built into PHP
-
Which of the following is a valid way to comment in Twig?
- A)
// This is a comment
- B)
/* This is a comment */
- C)
{# This is a comment #}
- D)
<!-- This is a comment -->
- A)
-
What is the purpose of the
|striptags
filter?- A) To remove all HTML tags from a string
- B) To escape HTML tags
- C) To format a string as HTML
- D) To convert a string to uppercase
Filters
-
Which filter would you use to format a number as currency?
- A)
format_currency
- B)
currency_format
- C)
number_format
- D)
money_format
- A)
-
What does the
|replace
filter do?- A) Replaces all occurrences of a substring with another string
- B) Replaces the first occurrence of a substring
- C) Replaces a string with a number
- D) None of the above
-
How do you apply multiple filters to a variable?
- A)
{{ variable|filter1|filter2|filter3 }}
- B)
{{ filter1(filter2(filter3(variable))) }}
- C)
{{ variable|filter1(filter2(filter3)) }}
- D) Both A and B
- A)
Tests
-
What test can be used to check if a variable is a string?
- A)
is_string
- B)
string
- C)
is iterable
- D)
is defined
- A)
-
How do you combine multiple tests in a single condition?
- A) Using
&&
and||
- B) Using
and
andor
- C) Using
&
and|
- D) Both A and B
- A) Using
Control Structures
-
What is the correct syntax for a
for
loop that iterates over an array in Twig?- A)
{% for item in array %}
- B)
{% loop item in array %}
- C)
{% each item in array %}
- D) None of the above
- A)
-
How do you create an
if
statement with multiple conditions in Twig?- A)
{% if condition1 and condition2 %}
- B)
{% if condition1 or condition2 %}
- C)
{% if condition1 and condition2 or condition3 %}
- D) All of the above
- A)
Variables and Expressions
-
What is the output of
{{ variable is not defined ? 'No' : 'Yes' }}
ifvariable
is defined?- A)
No
- B)
Yes
- C)
Error
- D)
null
- A)
-
How do you access an array element in Twig?
- A)
{{ array[index] }}
- B)
{{ array->index }}
- C)
{{ array.index }}
- D) Both A and C
- A)
Functions
-
Which function is used to get the size of an array in Twig?
- A)
count()
- B)
size()
- C)
length()
- D) All of the above
- A)
-
What is the purpose of the
dump
function in Twig?- A) To output a variable's value for debugging
- B) To clear the output buffer
- C) To format a string
- D) To include a template
Operators
-
What does the
not
operator do in Twig?- A) Negates a boolean value
- B) Checks if a variable is not defined
- C) Both A and B
- D) None of the above
-
Which of the following is a valid comparison operator in Twig?
- A)
!=
- B)
!==
- C)
>
- D) All of the above
- A)
Template Re-use
-
What is the purpose of the
extends
tag in Twig?- A) To include a template
- B) To define a base template
- C) To create a macro
- D) To define a block
-
How do you embed a template in another template in Twig?
- A)
{% include 'template.twig' %}
- B)
{% embed 'template.twig' %}
- C)
{% import 'template.twig' %}
- D)
{% extend 'template.twig' %}
- A)
Mixed Questions
-
What is the output of
{{ 'Hello World'|striptags }}
if the input is<b>Hello</b> World
?- A)
Hello World
- B)
<b>Hello</b> World
- C)
Hello
- D)
Error
- A)
-
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
- A)
-
Which of the following is a valid way to define a block in Twig?
- A)
{% block name %}
- B)
{% define block name %}
- C)
{% function name() %}
- D)
{% template name() %}
- A)
-
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
-
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
- 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) To join two hashes
-
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
-
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)
- A)
-
What is the output of
{{ [1, 2, 3]|length }}
?- A)
3
- B)
2
- C)
1
- D)
Error
- A)
-
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 %}
- A)
-
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 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
-
What is the output of
{{ 'Twig'|length }}
?- A)
4
- B)
3
- C)
5
- D)
Error
- 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 does the
|merge
filter do?- A) Merges two arrays
- B) Merges two hashes
- C) Merges two strings
- D) Both A and B
-
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
-
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 output of
{{ 'Hello World'|slice(6, 5) }}
?- A)
Hello
- B)
World
- C)
Hello World
- D)
Error
- A)
-
How do you create a conditional statement that checks if a variable is an object?
- A)
{% if variable is iterable %}
- B)
{% if variable is object %}
- C)
{% if variable is defined and variable is not null %}
- D) All of the above
- A)
-
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
-
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) %}
- A)
-
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
-
What is the output of
{{ 10 % 3 }}
?- A)
3
- B)
1
- C)
0
- D)
Error
- A)
-
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
-
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
|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
-
What is the output of
{{ 'Twig'|upper }}
?- A)
Twig
- B)
TWIG
- C)
twig
- D)
Error
- A)
Answers
Basic Concepts
- B) Twig provides a cleaner syntax and better separation of logic and presentation
- C)
{# This is a comment #}
- A) To remove all HTML tags from a string
Filters
- C)
number_format
- A) Replaces all occurrences of a substring with another string
- D) Both A and B
Tests
- A)
is_string
- D) Both A and B
Control Structures
- A)
{% for item in array %}
- D) All of the above
Variables and Expressions
- B)
Yes
- D) Both A and C
Functions
- D) All of the above
- A) To output a variable's value for debugging
Operators
- C) Both A and B
- D) All of the above
Template Re-use
- B) To define a base template
- B)
{% embed 'template.twig' %}
Mixed Questions
- A)
Hello World
- D) All of the above
- A)
{% block name %}
- A) Formats a date string
- C) Both A and B
- B) To join an array into a string
- C) Both A and B
Advanced Questions
- A)
value in array
- A)
3
- A)
{% block outer %}{% block inner %}{% endblock %}{% endblock %}
- A)
{% filter name %}
- A) To provide a default value if a variable is undefined
- C)
4
- A)
{% for key, value in hash %}
- D) Both A and B
- B) To reverse an array
-
A)
{% for key, value in hash %}
-
B)
World
- D) All of the above
- D) All of the above
- B)
{% for item in array|batch(2) %}
- B) Joins an array into a string
- B)
1
- D) All of the above
- A)
{% for key, value in hash %}
- A) To batch an array into smaller arrays
- B)
TWIG
Feel free to review your answers and see how well you understand Twig 3!