escape

    用来编码/转义一个变量为HTML,URL,单引号,16进制,16进制码,JavaScript和Email。缺省时为HTML转义。

参数位置 类型 必需? 可选值 缺省 描述
1stringNo html,htmlall,url,urlpathinfo,quotes,hex,hexentity,javascript,mail html 使用的转义格式
2stringNo ISO-8859-1,UTF-8以及任何由htmlentities()支持的字符集 ISO-8859-1 传递给htmlentities()的字符集编码

例子5-10. escape

<?php

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

?>

    模板为:

{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:'html'}    {* 转义& " ' < > *}
{$articleTitle|escape:'htmlall'} {* 转义所有的HTML实体 *}
{$articleTitle|escape:'url'}
{$articleTitle|escape:'quotes'}
<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
{$EmailAddress|escape:'mail'}    {* 将EMAIL转换为文本 *}
{'mail@example.com'|escape:'mail'}

    输出为:

'Stiff Opposition Expected to Casketless Funeral Plan'
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
%27Stiff+Opposition+Expected+to+Casketless+Funeral+Plan%27
\'Stiff Opposition Expected to Casketless Funeral Plan\'
<a href="mailto:%62%6f%..snip..%65%74">&#x62;&#x6f;&#x62..snip..&#x65;&#x74;</a>
smarty [AT] example [DOT] com
mail [AT] example [DOT] com

例子5-11. 其它示例

    PHP函数可以用作修饰符,只要$security允许。

{* "rewind"参数记录了当前位置 *}
<a href="{$SCRIPT_NAME}?page=foo&rewind={$smarty.server.REQUEST_URI|urlencode}">click here</a>

    该代码段可以用于Email,请同时参见{mailto}

{* Email地址被打乱 *}
   <a href="mailto:{$EmailAddress|escape:'hex'}">{$EmailAddress|escape:'mail'}</a>

    参见Smarty解析的转义{mailto}以及保护Email地址页。