首页 > 代码库 > smarty-2014-02-28

smarty-2014-02-28

使用smarty,在tpl文件中如何使用相对路径调用css&javascript文件,实际上这个相对路径的参照物就是以调用该tpl文件的php文件来写。

假如,我在index.php这个文件中调用template/index.tpl,那么index.tpl里面使用的css&javascript文件的相对路径就是index.php相对

于这些文件的路径。

display用法:

// from PHP script , for linux

$smarty->display("file:/export/templates/index.tpl");

$smarty->display("file:/path/to/my/templates/menu.tpl");

 

// from PHP script, for windows

$smarty->display("file:C:/export/templates/index.tpl");

$smarty->display("file:F:/path/to/my/templates/menu.tpl");

 

include用法:

{include file="模板存放的路径"}

//from within Smarty Template , for windows & for linux

{include file="file:file:D:/usr/local/share/templates/navigation.tpl"}

{include file="file="file:/usr/local/share/templates/navigation.tpl"}

 

foreach用法:

{foreach [name="the name of the foreach loop for accessing foreach properties"]  from="the name of the array you are looping through" item="the name of the variable that is current element" }

......

{/foreach}

还可以使用$smarty.foreach.foreachname.varname形式来访foreach的属性,foreachname指的是foreach中指定的name属性值;

varname:

iteration(iteration is used to display the current loop iteration, iteration always starts with 1 and incremented by one each iteration);

first(first is set to true if the current foreach iteration is the first one);

last(last is set to true if the current foreach iteration is the last one);

show(show is used as a parameter to foreach. show is a boolean value, true or false. If false, the foreach will not be displayed. If there is a foreachelse present, that will be alternately displayed.);

total(total is used to display the number of iterations that this foreach will loop. This can be used  inside or after the foreach.)