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
-
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
-
Which of the following is a valid Twig syntax for a variable?
- A)
{{ variable }} - B)
{% variable %} - C)
<< variable >> - D)
[variable]
- A)
-
What is the purpose of the
|escapefilter?- A) To remove whitespace
- B) To convert a string to lowercase
- C) To escape HTML characters
- D) To format a date
Filters
-
Which filter would you use to capitalize the first letter of a string?
- A)
capitalize - B)
upper - C)
title - D)
first
- A)
-
What does the
|lengthfilter 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
-
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) }}
- A)
Tests
-
Which test checks if a variable is empty?
- A)
empty - B)
null - C)
defined - D)
exists
- A)
-
How do you combine tests in Twig?
- A) Using
andandor - B) Using
&&and|| - C) Using
&and| - D) All of the above
- A) Using
Control Structures
-
What is the correct syntax for a
whileloop in Twig?- A)
{% while condition %} - B)
{% for condition %} - C)
{% loop condition %} - D) Twig does not support
whileloops.
- 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
includefunction in Twig?- A) To include a file
- B) To include a template
- C) To include a variable
- D) To include a string
Operators
-
What does the
inoperator 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
-
Which of the following is a valid operator for string comparison in Twig?
- A)
= - B)
== - C)
=== - D) Both B and C
- A)
Template Re-use
-
What is the purpose of the
blocktag in Twig?- A) To define a variable
- B) To create a reusable template section
- C) To include a template
- D) To extend a template
-
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' %}
- A)
Mixed Questions
-
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 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
- A)
-
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() %}
- A)
-
What does the
|datefilter 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
|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?- 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
|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 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
|mergefilter do?- A) Merges two arrays
- B) Merges two hashes
- C) Merges two strings
- D) Both A and B
-
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?
- 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(0, 5) }}?- A)
Hello - B)
Hello World - C)
World - 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)
-
What is the purpose of the
|sortfilter?- 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 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') %}
- A)
-
What is the purpose of the
|batchfilter?- 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?
- 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
{{ 10 % 3 }}?- A)
3 - B)
1 - C)
0 - 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?
- 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
|joinfilter do?- A) Joins two strings
- B) Joins an array into a string
- C) Joins two arrays
- D) Joins two hashes
Answers
Basic Concepts
- C) To serve as a template engine for PHP
- A)
{{ variable }} - C) To escape HTML characters
Filters
- A)
capitalize - C) Both A and B
- A)
{{ variable|filter1|filter2 }}
Tests
- A)
empty - A) Using
andandor
Control Structures
- D) Twig does not support
whileloops. - A)
{% for item in items %}{% for subitem in subitems %}{% endfor %}{% endfor %}
Variables and Expressions
- B)
No - A)
{{ object.property }}
Functions
- C)
now() - B) To include a template
Operators
- A) Checks if a value exists in an array
- D) Both B and C
Template Re-use
- B) To create a reusable template section
- A)
{% include 'fragment.twig' %}
Mixed Questions
- C)
HELLO - D) All of the above
- A)
{% macro 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
- A)
4 - A)
{% for key, value in hash %} - D) Both A and B
- B) To reverse an array
-
A)
{% for key, value in hash %} -
A)
Hello - A)
{% if variable is iterable %} - D) All of the above
- A)
{% for item in array|reverse %} - A) To batch an array into smaller arrays
- A)
{% for key, value in hash %} - B)
1 - A) To get the first element of an array
- B)
{% for item in array|batch(2) %} - B) Joins an array into a string
Feel free to review your answers and see how well you understand Twig 3!