escape

escape可用于将变量编码或转换成html, url, 单引号, 十六进制, 十六进制实体, javascript电邮地址。 默认是:html

参数顺序 类型 必选参数 允许取值 默认值 说明
1 string No html, htmlall, url, urlpathinfo, 单引号, 十六进制, 十六进制实体, javascript, 电邮地址 html 这是escape转换后的格式
2 string No ISO-8859-1, UTF-8, 和其他 htmlentities()支持的字符集 UTF-8 传递给htmlentities()的字符集类型
3 boolean No FALSE TRUE 两次转换实体,& 到 & (仅在 htmlhtmlall 使用)


Example 5.10. escape

  1. <?php
  2.  
  3. $smarty->assign('articleTitle',
  4. "'Stiff Opposition Expected to Casketless Funeral Plan'"
  5. );
  6. $smarty->assign('EmailAddress','smarty@example.com');
  7.  
  8. ?>
  9.  

下面是escape的例子和输出:

  1. {$articleTitle}
  2. 'Stiff Opposition Expected to Casketless Funeral Plan'
  3.  
  4. {$articleTitle|escape}
  5. &#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
  6.  
  7. {$articleTitle|escape:'html'} {* escapes & " ' < > *}
  8. &#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
  9.  
  10. {$articleTitle|escape:'htmlall'} {* escapes ALL html entities *}
  11. &#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
  12.  
  13. <a href="?title={$articleTitle|escape:'url'}">click here</a>
  14. <a
  15. href="?title=%27Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan%27">click here</a>
  16.  
  17. {$articleTitle|escape:'quotes'}
  18. \'Stiff Opposition Expected to Casketless Funeral Plan\'
  19.  
  20. <a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
  21. {$EmailAddress|escape:'mail'} {* this converts to email to text *}
  22. <a href="mailto:%62%6f%..snip..%65%74">&#x62;&#x6f;&#x62..snip..&#x65;&#x74;</a>
  23.  
  24. {'mail@example.com'|escape:'mail'}
  25. smarty [AT] example [DOT] com
  26.  


Example 5.11. escape的另一个例子

  1. {* "rewind"参数是当前URL地址 *}
  2. <a href="$my_path?page=foo&rewind=$my_uri|urlencode}">click here</a>
  3.  

这个方法很适合用在电邮地址上,另外可以参见 {mailto}

  1. {* 电邮地址编码 *}
  2. <a href="mailto:{$EmailAddress|escape:'hex'}">{$EmailAddress|escape:'mail'}</a>
  3.  

参见 Smarty编译转换, {mailto}混淆电邮地址

原文: https://www.smarty.net/docs/zh_CN/language.modifier.escape.tpl