Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Twig Documentation with understandable examples

Rating
-
Sold
-
Pages
38
Uploaded on
11-07-2023
Written in
2022/2023

The flexible, fast, and secure template engine for PHP which is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.

Institution
Course

Content preview

Twig – A modern
template engine
for PHP
A complete guidance with understandable examples and output




Imam Hussain – 4 years of experience in web development

, Twig – A modern template engine for PHP

The Twig code could be variables or expressions, which get replaced
with values when the template is rendered, and tags, which control the
logic of the template.
There are two kinds of tags in Twig:
• Logic tags: {% ... %}
• Output tags: {{ .... }}

Comments
To comment out one or more lines of code (or part of a line) use the
the {# #} syntax.

{# This will be a comment #}

<p>You can also comment out {# part of a line #}.</p>

{# This will be a
multi-line
comment.
#}

Setting Variables
{% set foo1 = 'foo' %}
{% set foo2 = [1, 2] %}
{% set foo3 = {'foo': 'bar'} %}
{{foo1}}
{{foo2[1]}}
{{foo3.foo}}
O/p: foo
2
bar

Filters
Variables can be modified by filters. Filters are separated from the
variable by a pipe symbol (|). Multiple filters can be chained.
{% apply upper %}
This text becomes uppercase
{% endapply %}

abs
The abs filter returns the absolute value.

{% set number = -10 %}
2

, Twig – A modern template engine for PHP

{{ number|abs }}
O/p : 10

batch
The batch filter "batches" items by returning a list of lists with the
given number of items. A second parameter can be provided and used
to fill in missing items:

{% set items = ['a', 'b', 'c'] %}
<table>
{% for row in items|batch(2, 'No item') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

O/p: <table>
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>c</td>
<td>No item</td>
</tr>
</table>

capitalize
The capitalize filter capitalizes a value. The first character will be
uppercase, all others lowercase:
{{ 'my first car'|capitalize }}
O/p : My first car

column
The column filter returns the values from a single column in the input
array.
{% set items = [{ 'fruit' : 'apple'}, {'fruit' : 'orange' }] %}
{% set fruits = items|column('fruit') %}
{{fruits[1]}}

3

, Twig – A modern template engine for PHP

O/p : orange

convert_encoding
The convert_encoding filter converts a string from
one encoding to another. The first argument is the
expected output charset and the second one is the
input charset:{{ “data” | convert_encoding('UTF-
8', 'iso-2022-jp') }} O/p : data

country_name
The country_name filter returns the country name given its ISO-3166
two-letter code:
{{ 'FR'|country_name }} O/p : France


currency_name
The currency_name filter returns the currency
name given its three-letter code:
{{ 'EUR'|currency_name }} O/p : Euro

currency_symbol
The currency_symbol filter returns the currency
symbol given its three-letter code:
{{{ 'JPY'|currency_symbol }} O/p : ¥

data_uri
The data_uri filter generates a URL using the data scheme

date
The date filter formats a date to a given format:
{{ "now"|date("d/m/Y") }} or {{ "now"|date(“d/m/Y”,
"Europe/Paris") }} O/p : 19/07/2022 or 19/07/2022

date_modify
The date_modify filter modifies a date with a given
modifier string:
{{ "now"|date_modify("+1 day")|date("d/m/Y") }}
O/p : 20/07/2022

4

Written for

Institution
Course

Document information

Uploaded on
July 11, 2023
Number of pages
38
Written in
2022/2023
Type
SUMMARY

Subjects

$8.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
imamhussainbojagar

Get to know the seller

Seller avatar
imamhussainbojagar Exam Questions
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
7
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions