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")
?>