smarty模板入门笔记

 

smarty笔记 javascript

一、smarty里面默认标签是{},在里面调用值。可是当解析的时候,会把javascript里面的花括号也解析成smarty里面的标签。所以,咱们须要自定义一下smarty的标签。
左边界标签:[left_delimiter] php

右边界标签:[right_delimiter] css

那咱们就能够在php文件中定义了: html

$smarty->left_delimiter = “{hd:”; java

$smarty->right_delimiter = “/}”; mysql

那么在模板html文件中就要使用{hd:$data}了。 jquery

注意:标签,本身定义,要定义一个好记的。本身看着办。 sql

二、  在模板中使用函数 数据库

第一种:使用内置的函数 数组

eg: {hd:$data|substr:0:2|strtoupper} 

 其中使用了substr()strtoupper()两个函数,函数之间用|来隔开。函数与参数和参数与参数用冒号:隔开。

第二种:使用自定义的函数

那么首先要写一个包含自定义方法的php文件,好比在smarty目录下创建一个lib文件夹,再在其下面创建function.php文件。而后在index.php中把该文件包含进来。include “./smarty/lib/function.php”

那咱们在function.php定义一个把文字描红的函数。代码以下:

<?php

       function red($str){

              return "<span style='color:red'>{$str}</span>";

       }

?>

eg: {hd:$data|substr:0:2|strtoupper|red} 

三、配置文件,建立一个smarty.php文件

<?php

/*经常使用的配置文件*/

       include "./smarty/Smarty.class.php";

       include "./smarty/lib/function.php";

       $smarty = new Smarty();

       $smarty->left_delimiter = "{hd:}";

       $smarty->right_delimiter = "}";

       $smarty->template_dir = "./smarty/tpl";

       $smarty->compile_dir = "./smarty/compile";

?>

四、我发现smarty的内置变量只能用原始定界符号{},不然会不显示。因而仍是用默认定界符。那么解决javascript{}smarty冲突的方法以下:

Smarty 提供了一个转义一段代码的标签:{literal}…{/literal}  javascript的代码写在这两个标签中间。

五、Smarty视图模板的复用

传统的PHP程序中是在PHP代码中使用<?php include “top.html”?>;包含进去.

smarty中,要在模板html文件中包含。{include file=”top.html”}

六、  section用法

七、  自定义任意smarty函数和标签

1、在pulgins里面,以modifier开头的,咱们复制一份,修改其中间的名字为本身须要的,而后重写里面的函数便可。

eg:其中substr_d是咱们新定义的函数名称,前缀smarty_modifier_不要动他。

<?php

function smarty_modifier_substr_d($string,$len)

{

    return mb_substr($string,0,$len,"utf-8");

}

?>

在模板中调用。如:{$bbs|substr_d:3}

2、还有一种是以function开头的,咱们一样弄一份,好比这里设置一个调用jquery插件的方法。

<?php

function smarty_function_jquery($params, &$smarty)

{

    return "<script type='text/javascript' src='jquery.min.js'></script>";

}

?>

在模板中调用。如:{jquery}

3、仍是以function开头的,定义一个标签,调用css文件。其中$params是一个数组,返回标签后面的值。如这里返回file的值houduwang.css给变量$cssFile. 与上面jquery不一样之处在于后面跟了参数。

<?php

function smarty_function_css($params, &$smarty)

{

     $cssFile = $params['file'];

     return "<link rel='stylesheet' type='text/css' href='{$cssFile}' />";

}

?>

在模板中调用。如:{css file=”houdunwang.css”}

八、  smarty操做数据库

function.php中定义操做数据的函数,并返回值.以下面:

<?php

       function query1($sql){

              static $db=null;

              if(is_null($db)){

                     mysql_connect("localhost","root","");

                     mysql_select_db("think");

                     mysql_query("set names utf8");

              }

              $result = mysql_query($sql);

              $rows = array();

              while($row = mysql_fetch_assoc($result)){

                     $rows[] = $row;

              }

              return $rows;

       }

      

       function query2($sql){

              static $db=null;

              if(is_null($db)){

                     mysql_connect("localhost","root","");

                     mysql_select_db("think");

                     mysql_query("set names utf8");

              }

              $result = mysql_query($sql);

              $arr =array();

              while($info = mysql_fetch_array($result)){

                     $arr[] = $info;

              }

              return $arr;

}

?>

九、  自定义block标签

plugins文件夹下面以block开头的文件,自定义一个叫作smarty_block_arclist.php的文件。而后写入如下代码:其中$content是指标签中包含的内容。

<?php

       function smarty_block_arclist($params, $content, &$smarty)

       {

       $row = isset($params['row'])?$params['row']:3;#显示条数

       $limit = "limit ".$row;

       $sql = "select * from hd_wish ".$limit;

       $result = mysql_query($sql);

       $rows = query($sql);

       if(!$rows){

              return false;

       }

       foreach($rows as $row){

              $tmp = $content;

              $fieldName = array_keys($row);#取得字段名

              foreach($fieldName as $name){

                     $tmp = str_replace("[\$field.".$name."]",$row[$name],$tmp);

              }

              $str.=$tmp;

       }

       return $str;

}

?>

而后在模板文件中写入如下标签块

{arclist row="3"}

       [$field.id] | [$field.username] <br>

{/arclist}

十、            smarty缓存cache使用

<?php

       header("Content-type:text/html;charset=utf-8");

       include "smarty.php";

       $smarty->caching = 1;//开启缓存

       $smarty->cache_dir = "cache";//缓存目录

       $smarty->cache_lifetime = 10;//缓存时间

       if(!$smarty->is_cached("8.html")){

              $data = query("select * from hd_wish");

              $smarty->assign("data",$data);

       }

       $smarty->display("8.html")

?>

相关文章
相关标签/搜索