Twig 3 Certification Test 9
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 purpose of Twig as a template engine?
- A) To manage databases
- B) To render HTML templates with dynamic content
- C) To handle HTTP requests
- D) To create web applications
-
Which of the following is the correct syntax to define a variable in Twig?
- A)
{% set variable = value %} - B)
{{ variable = value }} - C)
<< variable = value >> - D)
variable = value;
- A)
-
What is the purpose of the
|striptagsfilter?- A) To remove all HTML tags from a string
- B) To escape HTML characters
- C) To format a string as HTML
- D) To convert a string to uppercase
Filters
-
Which filter would you use to format a date in Twig?
- A)
format_date - B)
date - C)
date_format - D)
format
- A)
-
What does the
|replacefilter 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 using the pipe syntax?
- A)
{{ variable|filter1|filter2|filter3 }} - B)
{{ filter1(filter2(filter3(variable))) }} - C)
{{ variable|filter1(filter2(filter3)) }} - D) All of the above
- 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
andandor - C) Using
&and| - D) All of the above
- A) Using
Control Structures
-
What is the correct syntax for a
forloop 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 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.
- A)
Variables and Expressions
-
What is the output of
{{ variable is defined ? 'Yes' : 'No' }}ifvariableis not defined?- A)
Yes - B)
No - C)
Error - D)
null
- A)
-
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
- A)
Functions
-
Which function is used to get the current date in Twig?
- A)
current_date() - B)
date() - C)
now() - D)
today()
- A)
-
What is the purpose of the
dumpfunction 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
notoperator 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
extendstag 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 using the
isoperator?- 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 macro with arguments in Twig?
- A)
{% macro name(arg1, arg2) %} - B)
{% define macro name(arg1, arg2) %} - C)
{% function name(arg1, arg2) %} - D)
{% template name(arg1, arg2) %}
- A)
-
What does the
|datefilter do with the'Y-m-d'format?- A) Formats a date string in the
YYYY-MM-DDformat - B) Converts a string to a date object
- C) Returns the current date in the
YYYY-MM-DDformat - D) None of the above
- A) Formats a date string in the
-
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
|joinfilter 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
|slicefilter do with one argument?- A) Extracts a substring from a specific starting index
- B) Returns a portion of an array starting from a specific index
- C) Both A and B
- D) None of the above
Advanced Questions
-
How do you check if a string matches a regular expression using the
matchestest in Twig?- A)
{% if 'string' matches '/pattern/' %} - B)
{% if 'string' matches 'pattern' %} - C)
{% if 'string' matches ['pattern'] %} - D)
{% if 'string' matches '/pattern/' ignorecase %}
- A)
-
What is the output of
{{ [1, 2, 3]|reverse }}?- A)
[1, 2, 3] - B)
[3, 2, 1] - C)
1, 2, 3 - D)
3, 2, 1
- 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 using the alternative syntax?
- A)
{{ 'string'|myfilter }} - B)
{{ myfilter('string') }} - C)
{{ 'string'|myfilter('arg') }} - D) Both A and C
- A)
-
What is the purpose of the
|defaultfilter?- 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 and preserves the keys using the
keyskeyword?- 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 does the
|mergefilter do with arrays?- A) Merges two arrays into a single array
- B) Merges two hashes into a single hash
- C) Merges two strings into a single string
- D) None of the above
-
What is the purpose of the
|reversefilter?- 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 as variables using the
keyskeyword?- 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 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 a number using the
isoperator?- A)
{% if variable is number %} - B)
{% if variable is defined and variable is not null %} - C)
{% if variable is iterable %} - D)
{% if variable is divisible by(1) %}
- A)
-
What is the purpose of the
|batchfilter?- A) Batches an array into smaller arrays
- B) Batches a hash into smaller hashes
- C) Batches a string into smaller strings
- D) Batches a number into smaller numbers
-
How do you create a loop that iterates over an array in reverse order using the
|reversefilter?- A)
{% for item in array|reverse %} - B)
{% for item in reverse(array) %} - C)
{% for item in array|sort('desc') %} - D) Both A and B
- A)
-
What does the
|absfilter do?- A) Returns the absolute value of a number
- B) Formats a date
- C) Formats a number
- D) Formats a boolean
-
What is the output of
{{ 10 / 3 }}?- A)
3 - B)
3.33 - C)
3.3333333333 - D)
Error
- A)
-
What is the purpose of the
|firstfilter?- 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 an array and skips every second element using the
|batchfilter with a step size?- A)
{% for item in array|slice(2) %} - B)
{% for item in array|batch(2, true) %} - C)
{% for item in array|slice(0, 2) %} - D)
{% for item in array|slice(1, 2) %}
- A)
-
What is the purpose of the
|datefilter with the'U'format?- A) Formats a date string in the Unix timestamp format
- B) Converts a string to a date object
- C) Returns the current date and time in the Unix timestamp format
- D) None of the above
-
What is the output of
{{ 'twig'|capitalize }}?- A)
Twig - B)
TWIG - C)
twig - D)
Error
- A)
Answers
Basic Concepts
- C) To output unescaped HTML
- A)
{% block name %} - A) To format a string with placeholders
Filters
- A)
upper - D) All of the above
- A)
{{ variable|filter1|filter2|filter3 }}
Tests
- C)
is iterable - A)
{{ variable is defined and variable is not null }}
Control Structures
- A)
{% for key, value in hash %} - D) All of the above
Variables and Expressions
- B)
Yes - D) All of the above
Functions
- B)
now() - A) To output a variable's value for debugging
Operators
- A) Checks if a string starts with a specific substring
- D) All of the above
Template Re-use
- B) To define a base template
- C)
{% import 'template.twig' as macros %}
Mixed Questions
- B)
Hi Twig - D) All of the above
- A)
{% macro name(arg1, arg2) %} - A) Formats a date string
- D) All of the above
- B) To join an array into a string
- C) Both A and B
Advanced Questions
- A)
{% if 'string' matches '/pattern/' %} - B)
[3, 2, 1] - A)
{% block outer %}{% block inner %}{% endblock %}{% endblock %} - D) Both A and C
- A) To provide a default value if a variable is undefined
- A)
4 - A)
{% for key, value in hash %} - A) Merges two arrays into a single array
- B) To reverse an array
- A)
{% for key, value in hash %} - B)
World - A)
{% if variable is number %} - A) Batches an array into smaller arrays
- A)
{% for item in array|reverse %} - A) Returns the absolute value of a number
- C)
3.3333333333 - D) All of the above
- B)
{% for item in array|batch(2, true) %} - A) Formats a date string in the Unix timestamp format
- A)
Twig
Feel free to review your answers and see how well you understand Twig 3!
Citations: [1] https://www.programiz.com/c-programming/c-arrays [2] https://www.w3schools.com/c/c_arrays.php [3] https://www.geeksforgeeks.org/c-arrays/ [4] https://www.javatpoint.com/c-array [5] https://www.w3schools.com/c/c_pointers_arrays.php