Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

避免Smarty解析

有时候部分模板中的代码是不需要或者不希望被Smarty解析的, 比较典型的例子是嵌入在页面HTML中的Javascript或CSS代码。 问题通常发生在这些语言会经常使用{ 和 },但{ 和 }也恰好是Smarty的 定界符

Note

避免被解析的一个良好方式,是分离你的Javascript/CSS代码到单独的文件中, 然后在HTML中引入它们。而且这样也有利于浏览器进行缓存。 当你需要嵌入Smarty的变量或者函数到Javascript/CSS中,请参考下面的方式:

Smarty模板中,当{ 和 }定界符两边都是空格的时候,将会被自动忽略解析。 此特性可以通过设置Smarty的成员变量 $auto_literal为false来关闭。

Example 3.8. 使用自动忽略解析的特性


<script>
   // 下面的定界符两边都是空格,所以可以被自动忽略解析
   function foobar {
	alert('foobar!');
   }
   // 下面需要手动忽略解析
   {literal}
	function bazzy {alert('foobar!');}
   {/literal}
</script>
  
 

{literal}..{/literal}可以让块中间的内容忽略Smarty的解析。 在需要使用定界符的时候,可以通过 {ldelim}{rdelim}标签,或者 {$smarty.ldelim},{$smarty.rdelim}的变量来使用。

Smarty的默认定界符{ 和 }可以整齐地界定一般内容的显示。 然而你可以通过修改Smarty的 $left_delimiter $right_delimiter 的变量值,设置更适合的定界符。

Note

修改定界符会影响到全部模板代码和解析,请确保在修改前已清除了全部的缓存和编译文件。

Example 3.9. 改变定界符的例子


<?php

$smarty->left_delimiter = '<!--{';
$smarty->right_delimiter = '}-->';

$smarty->assign('foo', 'bar');
$smarty->assign('name', 'Albert');
$smarty->display('example.tpl');

?>

  

模板:


Welcome <!--{$name}--> to Smarty
<script language="javascript">
  var foo = <!--{$foo}-->;
  function dosomething() {
    alert("foo is " + foo);
  }
  dosomething();
</script>

  

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 11 plus 15? (Are you human?)

Advertisement