跳至内容

转义

escape 用于对变量编码或转义成 htmlurl单引号hexhexentityjavascriptmail。它的默认设置是 html

基本用法

{$myVar|escape}

参数

参数位置 类型 必需 可能值 默认值 描述
1 字符串 htmlhtmlallurlurlpathinfoquoteshexhexentityjavascriptmail html 这是要使用的转义格式。
2 字符串 ISO-8859-1UTF-8htmlentities() 支持的任何字符集 UTF-8 传递给 htmlentities() 及于其他函数的字符集编码。
3 布尔值 FALSE TRUE 双重编码实体,从 & 到 & (仅适用于 htmlhtmlall)

示例

<?php

$smarty->assign('articleTitle',
                "'Stiff Opposition Expected to Casketless Funeral Plan'"
                );
$smarty->assign('EmailAddress','smarty@example.com');

这些示例 escape 行,后面是输出

{$articleTitle}
'Stiff Opposition Expected to Casketless Funeral Plan'

{$articleTitle|escape}
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

{$articleTitle|escape:'html'}    {* escapes  & " ' < > *}
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

{$articleTitle|escape:'htmlall'} {* escapes ALL html entities *}
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

<a href="?title={$articleTitle|escape:'url'}">click here</a>
<a
href="?title=%27Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan%27">click here</a>

{$articleTitle|escape:'quotes'}
\'Stiff Opposition Expected to Casketless Funeral Plan\'

<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
{$EmailAddress|escape:'mail'}    {* this converts to email to text *}
<a href="mailto:%62%6f%..snip..%65%74">&#x62;&#x6f;&#x62..snip..&#x65;&#x74;</a>

{'mail@example.com'|escape:'mail'}
smarty [AT] example [DOT] com

{* the "rewind" parameter registers the current location *}
<a href="$my_path?page=foo&rewind={$my_uri|escape:url}">click here</a>

此代码段对于电子邮件很有用,但也参见 {mailto}

{* email address mangled *}
<a href="mailto:{$EmailAddress|escape:'hex'}">{$EmailAddress|escape:'mail'}</a>

另请参见 自动转义转义 Smarty 解析{mailto} 以及 混淆电子邮件地址 页面。