国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 Twig 的Filters學(xué)習(xí)

Twig 的Filters學(xué)習(xí)

Jun 13, 2016 am 10:46 AM
date filters format replace twig study support of at present filter

目前支持的過濾器包括

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過濾器
1.1版本新增時區(qū)支持,1.5版本增加了默認(rèn)的日期格式。
這個過濾器和php的date函數(shù)無限類似
{{ post.published_at|date("m/d/Y") }}?
{{ "now"|date("m/d/Y") }}?
{{ post.published_at|date("m/d/Y") }}
{{ "now"|date("m/d/Y") }}
如果想在格式里輸出字母,需要在每個字母前輸入\\
{{ post.published_at|date("F jS \\a\\t g:ia") }}?
{{ post.published_at|date("F jS \\a\\t g:ia") }}注意:經(jīng)過我的測試,不能輸入中文字符,這樣寫不行。。 {{ 'now'|date("F jS \\上\\午 g:ia") }}
你可以指定時區(qū)
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}?
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}
如果你提供的格式化字符串是不支持,會自動使用默認(rèn)格式 (F j, Y H:i)你可以用代碼修改這個默認(rèn)格式
$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過濾器
和php的printf函數(shù)一樣,用來替換占位符
{{ "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過濾器
這個自己看吧{{ "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過濾器
1.5版本新增過濾器。
他是php函數(shù) number_format的一個包裝 直接見函數(shù)參考吧
{{ 200.35|number_format }}?
{{ 200.35|number_format }}另外就是可以用php來修改默認(rèn)的格式
$twig = new Twig_Environment($loader);?
$twig->getExtension('core')->setNumberFormat(3, ',', '.');?
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setNumberFormat(3, ',', '.');
url_encode過濾器
這個直接使用 urlencode函數(shù)
{{ data|url_encode() }}?
{{ data|url_encode() }}


json_encode過濾器
直接使用json_encode函數(shù)
{{ data|json_encode() }}?
{{ data|json_encode() }}


convert_encoding過濾器
1.4版本新加內(nèi)容
轉(zhuǎn)換一個字符串,第一個參數(shù)是輸出編碼,第二個參數(shù)是輸入編碼
本函數(shù)依賴于iconv 或者mbstring 所以至少需要安裝一個
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}?
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}

title過濾器
會讓每個單詞的首字母大寫。
{{ 'my first car'|title }}?
?
{# outputs 'My First Car' #}?
{{ 'my first car'|title }}

{# outputs 'My First Car' #}
capitalize過濾器
會把字符串變成 首字母大寫,其余字母小寫的格式
{{ 'my first car'|capitalize }}?
?
{# outputs 'My first car' #}?
{{ 'my first car'|capitalize }}

{# outputs 'My first car' #}

nl2br過濾器
會把換行符\n 變成

{{ "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 過濾器
讓字符串變大小寫
striptags過濾器
直接使用的是strip_tags函數(shù)
join過濾器
這個我很喜歡,跟python的join一樣,用來將一個數(shù)組的內(nèi)容連接起來,并用指定的字符串分割。
{{ [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 過濾器
反轉(zhuǎn)一個數(shù)組,或者是一個實現(xiàn)了Iterator接口的對象
{% for use in users|reverse %}?
??? ...?
{% endfor %}?
{% for use in users|reverse %}
??? ...
{% endfor %}

length過濾器
返回一個數(shù)組或者字符串的長度
{% if users|length > 10 %}?
??? ...?
{% endif %}?
{% if users|length > 10 %}
??? ...
{% endif %}

sort過濾器
使用的是sort函數(shù)
{% for use in users|sort %}?
??? ...?
{% endfor %}?
{% for use in users|sort %}
??? ...
{% endfor %}
default過濾器
當(dāng)變量沒定義或者為空的時候,返回預(yù)先設(shè)置的內(nèi)容
{{ 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過濾器
返回key數(shù)組
{% for key in array|keys %}?
??? ...?
{% endfor %}?
{% for key in array|keys %}
??? ...
{% endfor %}

escape過濾器
主要轉(zhuǎn)義? & ' " 。并且它有個簡寫方式 e。
{{ user.username|escape }}?
{{ user.username|e }}?
{{ user.username|escape }}
{{ user.username|e }}還可以轉(zhuǎn)義 js
{{ user.username|escape('js') }}?
{{ user.username|e('js') }}?
{{ user.username|escape('js') }}
{{ user.username|e('js') }}實際上他使用的是php函數(shù)? htmlspecialchars

?

raw過濾器
用于在autoescape標(biāo)簽內(nèi)部,標(biāo)記出不需要轉(zhuǎn)義的內(nèi)容。
{% autoescape true %}?
??? {{ var|raw }} {# var won't be escaped #}?
{% endautoescape %}?
{% autoescape true %}
??? {{ var|raw }} {# var won't be escaped #}
{% endautoescape %}

merge過濾器
用來合并數(shù)組
{% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}?
?
{% set itemsitems = items|merge({ 'peugeot': 'car' }) %}?
?
{# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}?

摘自 jiaochangyun的專欄

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

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

Does PyCharm Community Edition support enough plugins? Does PyCharm Community Edition support enough plugins? Feb 20, 2024 pm 04:42 PM

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

Pros and Cons Analysis: A closer look at the pros and cons of open source software Pros and Cons Analysis: A closer look at the pros and cons of open source software Feb 23, 2024 pm 11:00 PM

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

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

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

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

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:

Learn the main function in Go language from scratch Learn the main function in Go language from scratch Mar 27, 2024 pm 05:03 PM

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

How does C++ software implement Chinese language support? How does C++ software implement Chinese language support? Mar 29, 2024 pm 12:15 PM

How does C++ software implement Chinese language support? With the process of globalization, more and more software needs to support multiple languages, including Chinese. In C++ development, implementing Chinese language support is not complicated and can be easily completed with only some basic skills and tools. This article will introduce how to implement Chinese language support in C++ software and provide specific code examples. 1. Use Unicode encoding. In order to support Chinese, you must first ensure that the software uses Unicode encoding internally. Unicode is a standard

How to learn PPT typesetting software well (Section 3) How to learn PPT typesetting software well (Section 3) Mar 20, 2024 pm 04:46 PM

1. This lesson mainly explains [1: Alignment Principle]. First, it will be analyzed from daily life, such as buildings, historical sites, etc. 2. [The role of alignment]: Highlight the content relationship and unify the page vision. 3. This lesson starts from [Analysis of actual cases] [Step 1: Delete excessive and inappropriate beautification and special effects; Step 2: Unify fonts and colors]. 4. First change the [Font to Microsoft YaHei] and then [Change the color of the page] to typeset as shown in the picture. 5. Then go to [Drawing of Timeline], insert [Straight Line - Modify Thickness, Color] and then continue to insert [Ring - Close Fill, Turn on Black Stroke] and then [Copy - Reduce Fill Black] [Select Two to Align 】Create a 'button effect' and then type it, the effect is as shown in the figure

See all articles