Twig Filters Learning_PHP Tutorial
Currently supported filters include
date format replace number_format url_encode json_encode convert_encoding title capitalize nl2br upper lower striptags join reverse length sort default keys escape raw merge
date filter
Version 1.1 adds time zone support, and version 1.5 adds a default date format.
This filter is infinitely similar to PHP’s date function
{{ post.published_at|date("m/d/Y") }}
{{ "now"|date("m/d/Y") }}
{{ post.published_at|date("m/d/Y") }}
{{ "now"|date("m/d/Y") }}
If you want to output letters in the format, you need to enter \
before each letter
{{ post.published_at|date("F jS \a\t g:ia") }}
{{ post.published_at|date("F jS \a\t g:ia") }} Note: After my test, Chinese characters cannot be entered, so writing this way will not work. . {{ 'now'|date("F jS \上\afternoon g:ia") }}
You can specify the time zone
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}
If the format string you provide is not supported, the default format (F j, Y H:i) will be automatically used. You can modify this default format with code
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setDateFormat('d/m/Y', '%d days');
format filter
Like PHP’s printf function, it is used to replace placeholders
{{ "I like %s and %s."|format(foo, "bar") }}
{# returns I like foo and bar
If the foo parameter equals to the foo string. #}
{{ "I like %s and %s."|format(foo, "bar") }}
{# returns I like foo and bar
If the foo parameter equals to the foo string. #}
replace filter
See this for yourself{{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }}
{# returns I like foo and bar
If the foo parameter equals to the foo string. #}
{{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }}
{# returns I like foo and bar
If the foo parameter equals to the foo string. #}
number_format filter
Version 1.5 adds new filters.
It is a wrapper for the PHP function number_format. Please refer to the function reference directly
{{ 200.35|number_format }}
{{ 200.35|number_format }} In addition, you can use php to modify the default format
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setNumberFormat(3, ',', '.');
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setNumberFormat(3, ',', '.');
url_encode filter
This directly uses the urlencode function
{{ data|url_encode() }}
{{ data|url_encode() }}
json_encode filter
Use the json_encode function directly
{{ data|json_encode() }}
{{ data|json_encode() }}
convert_encoding filter
New content added in version 1.4
Convert a string, the first parameter is the output encoding, the second parameter is the input encoding
This function depends on iconv or mbstring so at least one needs to be installed
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
title filter
Capitalizes the first letter of each word.
{{ 'my first car'|title }}
{# outputs 'My First Car' #}
{{ 'my first car'|title }}
{# outputs 'My First Car' #}
capitalize filter
The string will be formatted with the first letter in uppercase and the remaining letters in lowercase
{{ 'my first car'|capitalize }}
{# outputs 'My first car' #}
{{ 'my first car'|capitalize }}
{# outputs 'My first car' #}
nl2br filter
will change the newline character n into
{{ "I like Twig.nYou will like it too."|nl2br }}
{# outputs
I like Twig.
You will like it too.
#}
{{ "I like Twig.nYou will like it too."|nl2br }}
{# outputs
I like Twig.
You will like it too.
#}
upper lower filter
Make the string uppercase and lowercase
striptags filter
The strip_tags function is used directly
join filter
I like this very much. It is the same as Python's join. It is used to connect the contents of an array and split it with a specified string.
{{ [1, 2, 3]|join }}
{# returns 123 #}
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}
{{ [1, 2, 3]|join }}
{# returns 123 #}
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}
reverse filter
Reverse an array, or an object that implements the Iterator interface
{% for use in users|reverse %}
...
{% endfor %}
{% for use in users|reverse %}
...
{% endfor %}
length filter
Returns the length of an array or string
{% if users|length > 10 %}
...
{% endif %}
{% if users|length > 10 %}
...
{% endif %}
sort filter
The sort function is used
{% for use in users|sort %}
...
{% endfor %}
{% for use in users|sort %}
...
{% endfor %}
default filter
When the variable is undefined or empty, the preset content is returned
{{ var|default('var is not defined') }}
{{ var.foo|default('foo item on var is not defined') }}
{{ var['foo']|default('foo item on var is not defined') }}
{{ ''|default('passed var is empty') }}
{{ var|default('var is not defined') }}
{{ var.foo|default('foo item on var is not defined') }}
{{ var['foo']|default('foo item on var is not defined') }}
{{ ''|default('passed var is empty') }}
keys filter
Return key array
{% for key in array|keys %}
...
{% endfor %}
{% for key in array|keys %}
...
{% endfor %}
escape filter
The main escape is & < > ' ". And it has an abbreviation e.
{{ user.username|escape }}
{{ user.username|e }}
{{ user.username|escape }}
{{ user.username|e }} can also be escaped js
{{ user.username|escape('js') }}
{{ user.username|e('js') }}
{{ user.username|escape('js') }}
{{ user.username|e('js') }} Actually he is using the php function htmlspecialchars
raw filter
Used to mark content that does not need to be escaped inside the autoescape tag.
{% autoescape true %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
{% autoescape true %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
merge filter
Used to merge arrays
{% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
{% set itemsitems = items|merge({ 'peugeot': 'car' }) %}
{# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
Excerpted from jiaochangyun’s column

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Pros and cons of open source software: Understanding the pros and cons of open source projects requires specific code examples In today’s digital age, open source software is getting more and more attention and respect. As a software development model based on the spirit of cooperation and sharing, open source software is widely used in different fields. However, despite the many advantages of open source software, there are also some challenges and limitations. This article will delve into the pros and cons of open source software and demonstrate the pros and cons of open source projects through specific code examples. 1. Advantages of open source software 1.1 Openness and transparency Open source software

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org

Nowadays, many Windows 10 laptops have fingerprint recognition function, which can directly unlock the device through fingerprint. However, due to a problem with the biometric device driver, a fingerprint reader that supports Windows cannot be found. Today, I will help you solve this problem. What to do if you cannot find a fingerprint reader that supports Windows 1. First, right-click the "Computer" icon on the desktop, click "Manage" in the pop-up option box to enter the computer management page, and then click "Device Manager". 2. Find "Biometric Device" in the expansion list. After expanding it, you can find an exclamation mark warning in front of the faulty fingerprint reader. 3. Then right-click the device and click "Properties" in the pop-up window. At this time, it will prompt "The device cannot be started." 4. Then right

Original author: Minty, encryption KOL Original compilation: Shenchao TechFlow If you know how to use it, Dune is an all-in-one alpha tool. Take your research to the next level with these 20 Dune dashboards. 1. TopHolder Analysis This simple tool developed by @dcfpascal can analyze tokens based on indicators such as holders’ monthly activity, number of unique holders, and wallet profit and loss ratio. Visit link: https://dune.com/dcfpascal/token-holders2. Token Overview Metrics @andrewhong5297 created this dashboard which provides a way to evaluate tokens by analyzing user actions
