Smarty核心内容:内建函数

config_load 该函数用于从配置文件中加载变量
{config_load file="colors.conf"}

<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
 <tr bgcolor="{#rowBgColor#}">
  <td>First</td>
  <td>Last</td>
  <td>Address</td>
 </tr>
</table>
</body>
</html>


 

include_php

include_php 是解决模板部件化的好方法,它使得 php 代码从模板文件中被分离出来. 举个例子:假设有一个从数据库中动态取出数据用于显示站点导航的模板,你可以将得数据内容的 php 逻辑部分分离出来保存在一个单独的文件夹下,并在模板开始的位置包含该 php 脚本. 那么就可以在任何地方包含此模板而不用担心之前数据库信息是否已被程序取出.

<?php

 // load in variables from a mysql db and assign them to the template
 // 从mysql数据库中取得数据,将数据赋给模板变量 require_once("MySQL.class.php");
 $sql = new MySQL;
 $sql->query("select * from site_nav_sections order by name",SQL_ALL);
 $this->assign('sections',$sql->record);

?>



index.tpl
---------

{* absolute path, or relative to $trusted_dir *}
{* 绝对路径或 $trusted_dir 的相对路径 *}
{include_php file="/path/to/load_nav.php"}

{foreach item="curr_section" from=$sections}
 <a href="{$curr_section.url}">{$curr_section.name}</a><br>
{/foreach}


 

insert
Insert 函数类似欲 include 函数,不同之处是 insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.

例如你在页面上端使用一个带有广告条位置的模板,广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接,同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定:#banner_location_id# 和 #site_id# 值(从配置文件中取),同时需要一个函数取广告条的内容信息.

index.php

<?php

function insert_getBanner($array){
 print_r($array);//$lid是个数组
 return $array['lid'];
}
?>


insert 函数演示

{* example of fetching a banner *}
{insert name="getBanner" lid=#banner_location_id# sid=#site_id#}


使用ldelim,rdelim,方便输出大括号。

eq.

{* this will print literal delimiters out of the template *}
{ldelim}funcname{rdelim} is how functions look in Smarty!
OUTPUT:
{funcname} is how functions look in Smarty!

literal
Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 {literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示.

{literal}
 <script language=javascript>

  <!--
  function isblank(field) {
  if (field.value == '') 
  { return false; }
  else
  {
  document.loginform.submit();
  return true;
  }
  }
  // -->

 </script>
{/literal}



 
php
php 标签允许在模板中直接嵌入 php 脚本。

{php}
  // including a php script directly
  // from the template.
  include("/path/to/display_weather.php");
{/php}
 



 

strip
Smarty 在显示前将除区任何位于 {strip}{/strip} 标记中数据的首尾空格和回车.

{* the following will be all run into one line upon output *}
{strip}
<table border=0>
 <tr>
  <td>
   <A HREF="{$url}">
   <font color="red">This is a test</font>
   </A>
  </td>
 </tr>
</table>
{/strip}


OUTPUT:
 <table border=0><tr><td><A HREF="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值