Twig 的 tags学习(中文) 之二_PHP教程
set标签
主要是用来给变量赋值的。
{% set foo = 'foo' %}
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
{% set foo = 'foo' ~ 'bar' %}
{% set foo, bar = 'foo', 'bar' %}
{% set foo = 'foo' %}
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
{% set foo = 'foo' ~ 'bar' %}
{% set foo, bar = 'foo', 'bar' %}
其中 'foo'~'bar' 这个我没怎么看明白,测试了一下,可能是字符串连接的。
set还有一种用法,就是把 块内的内容赋值给变量
{% set foo %}
...
{% endset %}
{% set foo %}
...
{% endset %}
extends标签
这个标签用来表示本模板继承自另外一个模板。和php一样,twig不支持多重继承,所以你只能有一个extends标签,而且要在模板的最上方。
我们先来定义一个“基模板” base.html 他就像一个骨架一个。
{% block head %}
{% endblock %}
{% block head %}
{% endblock %}
{% block %}标签定义了4个区块(block head, block title, block content, block footer),可以让子模板来填充内容。block的作用就是告诉模板引擎,这里面的内容可以被子模板覆盖。
一个子模板大概类似于这样的
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ parent() }}
{% endblock %}
{% block content %}
Index
Welcome on my awesome homepage.
{% endblock %}
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ parent() }}
{% endblock %}
{% block content %}
Index
Welcome on my awesome homepage.
{% endblock %}
extends是非常关键的,它告诉模板引擎,本模板继承自另一个模板(base.html)。当模板引擎解析到本模板时,会首先载入父模板。extends标签应该是模板内的第一个标签。
如果子模板没有定义block footer ,那么父模板会用默认值代替。
注意:block标签的名字是不能重复的。如果你想让同一个block多次打印。可以使用block函数
{{ block('title') }}
{% block body %}{% endblock %}
{{ block('title') }}
{% block body %}{% endblock %}
父block
也许你会需要 父block的内容。可以使用parent函数,这很有用比如你想往一个block里添加内容而不是覆盖时。
{% block sidebar %}
Table Of Contents
...
{{ parent() }}
{% endblock %}
{% block sidebar %}
Table Of Contents
...
{{ parent() }}
{% endblock %}
命名endblock
模板引擎 允许你命名结束标记,这样可读性会提高很多。但个人觉得没啥用处。
{% block sidebar %}
{% block inner_sidebar %}
...
{% endblock inner_sidebar %}
{% endblock sidebar %}
{% block sidebar %}
{% block inner_sidebar %}
...
{% endblock inner_sidebar %}
{% endblock sidebar %}
嵌套block
允许你嵌套生成block ,来形成更复杂的block
{% for item in seq %}
{% endfor %}
{% for item in seq %}
{% endfor %}
简写block
以下这两种写法是等效的
{% block title %}
{{ page_title|title }}
{% endblock %}
{% block title page_title|title %}
{% block title %}
{{ page_title|title }}
{% endblock %}
{% block title page_title|title %}
动态继承
你可以用一个变量来继承不同的模板。
{% extends some_var %}
{% extends some_var %}
如果变量是一个twig模板对象,也可以。
$layout = $twig->loadTemplate('some_layout_template.twig');
$twig->display('template.twig', array('layout' => $layout));
$layout = $twig->loadTemplate('some_layout_template.twig');
$twig->display('template.twig', array('layout' => $layout));
1.2版本更新 你可以传递一个数组,twig会选择第一个存在的模板,来继承。
{% extends ['layout.html', 'base_layout.html'] %}
{% extends ['layout.html', 'base_layout.html'] %}
条件继承
这个很简单自己看吧,
{% extends standalone ? "minimum.html" : "base.html" %}
{% extends standalone ? "minimum.html" : "base.html" %}
block标签
参见 extends标签
include标签
载入一个模板,返回渲染的内容。载入的模板可以使用当前模板的变量{% include 'header.html' %}
Body
{% include 'footer.html' %}
{% include 'header.html' %}
Body
{% include 'footer.html' %}
你可以给模板添加变量
{# the foo template will have access to the variables from the current context and the foo one #}
{% include 'foo' with {'foo': 'bar'} %}
{% set vars = {'foo': 'bar'} %}
{% include 'foo' with vars %}
{# the foo template will have access to the variables from the current context and the foo one #}
{% include 'foo' with {'foo': 'bar'} %}
{% set vars = {'foo': 'bar'} %}
{% include 'foo' with vars %}
你也可以使用 only 关键字 来禁止载入的模板使用当前模板的变量,只能使用include 时with的变量{# only the foo variable will be accessible #}
{% include 'foo' with {'foo': 'bar'} only %}
{# no variable will be accessible #}
{% include 'foo' only %}
{# only the foo variable will be accessible #}
{% include 'foo' with {'foo': 'bar'} only %}
{# no variable will be accessible #}
{% include 'foo' only %}
载入的模板名也可以是一个twig表达式
{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}
{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}
也可以用twig模板对象
$template = $twig->loadTemplate('some_template.twig');
$twig->loadTemplate('template.twig')->display(array('template' => $template));
$template = $twig->loadTemplate('some_template.twig');
$twig->loadTemplate('template.twig')->display(array('template' => $template));
1.2版本新加内容,可以在模板加上 ignore missing 关键字,这样当模板不存在的时候就不会引发错误。
{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with {'foo': 'bar} %}
{% include "sidebar.html" ignore missing only %}
{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with {'foo': 'bar} %}
{% include "sidebar.html" ignore missing only %}1.2版本新加内容,你可以给include传递一个数组,他会自动载入第一个存在的模板{% include ['page_detailed.html', 'page.html'] %}
{% include ['page_detailed.html', 'page.html'] %}
import 标签
twig允许把一些常用的代码放入到macros(宏)里,这些macros被不同的模板导入。
有两种方法导入模板,你可以导入整个模板到一个变量里,或者只导入需要的几个macros
假如我们有个助手模块,来帮助我们渲染表单(forms.html)
{% macro input(name, value, type, size) %}
{% endmacro %}
{% macro textarea(name, value, rows) %}
{% endmacro %}
{% macro input(name, value, type, size) %}
{% endmacro %}
{% macro textarea(name, value, rows) %}
{% endmacro %}
最简单,最灵活的办法就是导入整个模板。(把模板导入到 forms变量里)
{% import 'forms.html' as forms %}
- Username
- {{ forms.input('username') }}
- Password
- {{ forms.input('password', null, 'password') }}
{{ forms.textarea('comment') }}
{% import 'forms.html' as forms %}
- Username
- {{ forms.input('username') }}
- Password
- {{ forms.input('password', null, 'password') }}
{{ forms.textarea('comment') }}
或者你可以导入模板的名字到当前的名字空间下。 (导入input,textarea 并把input重名为input_field)
{% from 'forms.html' import input as input_field, textarea %}
- Username
- {{ input_field('username') }}
- Password
- {{ input_field('password', '', 'password') }}
{{ textarea('comment') }}
{% from 'forms.html' import input as input_field, textarea %}
- Username
- {{ input_field('username') }}
- Password
- {{ input_field('password', '', 'password') }}
{{ textarea('comment') }}
如果是当前模板内定义的macros,那就不必导入了,直接使用特殊变量_self
{# index.html template #}
{% macro textarea(name, value, rows) %}
{% endmacro %}
{{ _self.textarea('comment') }}
{# index.html template #}
{% macro textarea(name, value, rows) %}
{% endmacro %}
{{ _self.textarea('comment') }}
那么你仍然可以导入_self到一个变量里,尽管这看起来很。。。没用。。
{# index.html template #}
{% macro textarea(name, value, rows) %}
{% endmacro %}
{% import _self as forms %}
{{ forms.textarea('comment') }}
{# index.html template #}
{% macro textarea(name, value, rows) %}
{% endmacro %}
{% import _self as forms %}
{{ forms.textarea('comment') }}
from标签
参见 import标签
摘自 jiaochangyun的专栏

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使命召喚戰區作為全新上線的一款手遊,有很多的玩家都非常的好奇要怎麼樣才能夠將這款遊戲的語言設定為中文,其實非常的簡單,玩家只需要下載中文的語言包,隨後進行使用就可以進行修改了,詳細的內容可以在這篇中文設定方法介紹之中進行了解,讓我們一起來看看吧。使命召喚戰區手遊怎麼設定中文1、先進入遊戲,點選介面右上角的設定圖示。 2.在出現的選單列中,找到【Download】這個選項並且點選。 3.在這個頁面中選擇【SIMPLIFIEDCHINESE】(簡體中文),就可以對簡體中文的安裝包進行下載了。 4.回到設

VSCode設定中文:完整指南在軟體開發中,VisualStudioCode(簡稱VSCode)是一個常用的整合開發環境。對於使用中文的開發者來說,將VSCode設定為中文介面可以提升工作效率。本文將為大家提供一個完整的指南,詳細介紹如何將VSCode設定為中文介面,並提供具體的程式碼範例。第一步:下載安裝語言包開啟VSCode後,點選左

Excel表格是現在很多人都在使用的辦公室軟體之一,有些使用者因為電腦是win11系統,因此顯示的是英文介面,想要切換成中文介面,但是不知道該怎麼操作,針對這個問題,本期小編就來為廣大用戶們回答,一起來看看今日軟體教學所分享的內容。 Excel切換中文操作教學: 1、進入軟體,點選頁面上方工具列左側的「File」選項。 2、在下方給出的選項中選擇「options」。 3、進入新介面後,點選左側的「language」選項

解決PHP寫入txt檔案中文亂碼的技巧隨著網路的快速發展,PHP作為一種廣泛應用的程式語言,被越來越多的開發者所使用。在PHP開發中,經常需要對文字檔案進行讀寫操作,其中包括寫入中文內容的txt檔案。然而,由於編碼格式的問題,有時會導致寫入的中文出現亂碼。本文將介紹一些解決PHP寫入txt檔案中文亂碼的技巧,並提供具體的程式碼範例。問題分析在PHP中,文本

在word編輯文字內容時,有時會需要輸入公式符號。有的小夥子們不知道在word根號輸入的方法,小面就讓小編跟小夥伴們一起分享下word根號輸入的方法教學。希望對小夥伴們有幫助。首先,開啟電腦上的Word軟體,然後開啟要編輯的文件,並將遊標移到需要插入根號的位置,參考下方的圖片範例。 2.選擇【插入】,再選擇符號裡的【公式】。如下方圖片紅色圈的部分內容所示:3.接著選擇下方的【插入新公式】。如下方圖片紅色圈的部分內容所示:4.選擇【根式】,再選擇適當的根號。如下方圖片紅色圈的部分內容所示:

標題:從零開始學習Go語言中的main函數Go語言作為一種簡潔、高效的程式語言,備受開發者青睞。在Go語言中,main函數是一個入口函數,每個Go程式都必須包含main函數作為程式的入口點。本文將從零開始介紹如何學習Go語言中的main函數,並提供具體的程式碼範例。一、首先,我們需要安裝Go語言的開發環境。可前往官方網站(https://golang.org

步驟:對於 IDE,可透過開啟設置,找到語言設置,選擇中文,並儲存變更。對於非 IDE 應用程序,可尋找設定或選項,選擇語言設置,變更為中文,並儲存變更。

在瀏覽抖音作品時,我們常常可以看到標籤後面有時鐘圖示。那麼,這個時鐘到底是什麼呢?本文將圍繞著「抖音標籤後面的時鐘是什麼」展開討論,希望為您的抖音使用提供一些有益的參考。一、抖音標籤後面的時鐘是什麼?抖音會推出一些熱門話題挑戰,用戶參與時會在標籤後看到一個時鐘圖標,這代表作品正在參與話題挑戰,並顯示挑戰的剩餘時間。對於一些具有時效性的內容,如假日、特殊活動等,抖音會在標籤後面附上時鐘圖標,提醒使用者該內容的有效期限。 3.熱門標籤:當某個標籤變得熱門時,抖音會在標籤後面加上時鐘圖標,表示這個標籤正
