``autoescape``
==============
無論自動轉義是否啟用,你都可以使用``autoescape``標簽,標注是否將模版的某一節進行轉義:
{# 以下語法在 Twig 1.8 以上可用 —— 查看下方關于之前版本的提示 #}
{% autoescape %}
此處內容以HTML轉義策略進行自動轉義
{% endautoescape %}
{% autoescape 'html' %}
此處內容以HTML轉義策略進行自動轉義
{% endautoescape %}
{% autoescape 'js' %}
此處內容以JS轉義策略進行自動轉義
{% endautoescape %}
{% autoescape false %}
此處的內容以原本的樣子輸出,不轉義
{% endautoescape %}
提示:
> 在Twig1.8前,語法有所不同
> ~~~
> {% autoescape true %}
> 此處內容以HTML轉義策略進行自動轉義
> {% endautoescape %}
> {% autoescape false %}
> 此處的內容以原本的樣子輸出,不轉義
> {% endautoescape %}
> {% autoescape true js %}
> 此處內容以JS轉義策略進行自動轉義
> {% endautoescape %}
> ~~~
如果自動轉義已被棄用,那么除了明確標注為安全的值以外,所有東西都會默認地被轉義。使用`raw`過濾器在模板中標注:
{% autoescape %}
{{ safe_value|raw }}
{% endautoescape %}
函數返回的模板數據(比如`macro`和`parent`函數)總是會返回安全標簽。
注意:
> Twig足夠智能,不會再次轉義已由`escape`過濾器轉義過的值。
注意:
> Twig 不會轉義靜態表達式:
> {% set hello = "<strong>Hello</strong>" %}
> {{ hello }}
> {{ "<strong>world</strong>" }}
> 將會被渲染為 "`<strong>Hello</strong>` **world**".
注意:
> 面向開發者 這一章介紹了更多關于何時、如何使用自動轉義的信息。
- 首頁
- 目錄
- 介紹
- 安裝
- 面向模板設計師
- 面向開發者
- 擴展 Twig
- Twig的內部構建
- 棄用的特性
- 使用技巧
- 代碼規范
- 標簽 tags
- autoescape
- block
- do
- embed
- extends
- filter
- flush
- for
- from
- if
- import
- include
- macro
- sandbox
- set
- spaceless
- use
- verbatim
- 過濾器
- abs
- batch
- capitalize
- convert_encoding
- date
- date_modify
- default
- escape
- first
- format
- join
- json_encode
- keys
- last
- length
- lower
- merge
- nl2br
- number_format
- raw
- replace
- reverse
- round
- slice
- sort
- split
- striptags
- title
- trim
- upper
- url_encode
- 函數
- attribute
- block
- constant
- cycle
- date
- dump
- include
- max
- min
- parent
- random
- range
- source
- template_from_string
- 測試
- constant
- defined
- divisibleby
- empty
- even
- iterable
- null
- odd
- sameas