Twig's tags learning (Chinese) Part 1_PHP tutorial
Twig is a simple yet powerful template. I’m learning SF so take a look at her.
The source of this article is http://twig.sensiolabs.org/doc/tags/index.html
Currently supported tags include
for if macro filter set extends block include import from use spaceless autoescape raw flush do
twig is divided into 3 types in html
{{...}} directly outputs the variables
{#...#} comment tag
{%...%} command tags are what we need to learn
for tag
This is the simplest, it is a loop.
Array-based loop
Members
{% for user in users %}
{% endfor %}
Members
{% for user in users %}
{% endfor %}
For loops based on numbers, special attention should be paid to the fact that 0-10, which is 11 numbers, will be output here.
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
Letter-based loop
* {{ letter }}
{% endfor %}
{% for letter in 'a'..'z' %}
* {{ letter }}
{% endfor %}
Variables inside the loop body
loop.length, loop.revindex, loop.revindex0, loop.last These values are only valid when the loop is a PHP array or a class that implements the Countable interface.
Add a condition
Unlike PHP, break and continue statements are not supported inside loops. You can only skip some loops through filters, like this
{% for user in users if user.active %}
{% endfor %}
{% for user in users if user.active %}
{% endfor %}
else branch
If users is an empty array, no user found will be output.
-
{% for user in users %}
{% else %}
{% endfor %}
{% for user in users %}
{% else %}
{% endfor %}
Loop by keys
Members
{% for key in users|keys %}
{% endfor %}
Members
{% for key in users|keys %}
{% endfor %}
Loop by keys, values
Members
{% for key, user in users %} {% endfor %}
Members
{% for key, user in users %}
{% endfor %}
if tag
Needless to say, just look at the example {% if users %}
{% for user in users %}
{% endfor %}
{% endif %}
{% if kenny.sick %}
Kenny is sick.
{% elseif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
{% if users %}
- {{ user.username|e }}
{% for user in users %}
{% endfor %}
{% endif %}
{% if kenny.sick %}
Kenny is sick.
{% elseif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
macro tag
Macro (macro tag) is similar to functions in other languages and is often used to fill in html tags. The following is an example to render
{% macro input(name, value, type, size) %}
{% endmacro %}
{% macro input(name, value, type, size) %}
{% endmacro %}
macro与函数的不同之处在于:
1、参数的默认值是通过macro块内部的 default过滤器来定义的。
2、参数总是可选的。
另外,就跟php函数一样,macro内部是无法使用外部的变量的。但你可以传递一个特殊变量_context作为参数来获取整个内容。
macro可以被定义在任何的模板内,但在你使用之前需要使用 imported
{% import "forms.html" as forms %}
{% import "forms.html" as forms %}然后就可以这样使用了
{{ forms.input('username') }}
{{ forms.input('password', null, 'password') }}
{{ forms.input('username') }}
{{ forms.input('password', null, 'password') }}
如果你要在定义macro的模板里使用,就不需要imported 可以使用特殊变量_self
{{ _self.input('username') }}
{{ _self.input('username') }}
如果你要定义一个macro里 包含另一个macro,并且两个macro在同一个文件里,可以使用特殊变量_self
{% macro input(name, value, type, size) %}
{% endmacro %}
{% macro wrapped_input(name, value, type, size) %}
{{ _self.input(name, value, type, size) }}
{% endmacro %}
{% macro input(name, value, type, size) %}
{% endmacro %}
{% macro wrapped_input(name, value, type, size) %}
{{ _self.input(name, value, type, size) }}
{% endmacro %}
如果两个macro在不同的文件里,你需要使用import
{# forms.html #}
{% macro input(name, value, type, size) %}
{% endmacro %}
{# shortcuts.html #}
{% macro wrapped_input(name, value, type, size) %}
{% import "forms.html" as forms %}
{{ forms.input(name, value, type, size) }}
{% endmacro %}
{# forms.html #}
{% macro input(name, value, type, size) %}
{% endmacro %}
{# shortcuts.html #}
{% macro wrapped_input(name, value, type, size) %}
{% import "forms.html" as forms %}
{{ forms.input(name, value, type, size) }}
{% endmacro %}
filter tag
Just apply a filter to the entire block
{% filter upper %}
This text becomes uppercase
{% endfilter %}
{% filter upper %}
This text becomes uppercase
{% endfilter %}
{% filter lower|escape %}
SOME TEXT
{% endfilter %}
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



Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

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:

Regarding PPT masking, many people must be unfamiliar with it. Most people do not understand it thoroughly when making PPT, but just make it up to make what they like. Therefore, many people do not know what PPT masking means, nor do they understand it. I know what this mask does, and I don’t even know that it can make the picture less monotonous. Friends who want to learn, come and learn, and add some PPT masks to your PPT pictures. Make it less monotonous. So, how to add a PPT mask? Please read below. 1. First we open PPT, select a blank picture, then right-click [Set Background Format] and select a solid color. 2. Click [Insert], word art, enter the word 3. Click [Insert], click [Shape]

Steps: For IDE, open the settings, find the language settings, select Chinese, and save the changes. For non-IDE applications, find the setting or option, select the language setting, change to Chinese, and save the changes.

C++ template specializations affect function overloading and rewriting: Function overloading: Specialized versions can provide different implementations of a specific type, thus affecting the functions the compiler chooses to call. Function overriding: The specialized version in the derived class will override the template function in the base class, affecting the behavior of the derived class object when calling the function.

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
