欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

smarty模板的使用方法實(shí)例分析

 更新時(shí)間:2019年09月18日 11:59:36   作者:koastal  
這篇文章主要介紹了smarty模板的使用方法,結(jié)合實(shí)例形式分析了Smarty模板基本設(shè)置、使用方法與操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了smarty模板的使用方法。分享給大家供大家參考,具體如下:

這里以smarty3為例

首先, 在官網(wǎng)下載smarty3模板文件,然后解壓。

在解壓之后的文件夾中,libs是smarty模板的核心文件,demo里面有示例程序。

我們把libs文件夾復(fù)制到我們的工作目錄,然后重命名為smarty。

假設(shè)我們?cè)赾ontroller目錄下的index.php中使用smarty模板。

index.php

<?php
require '../smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false;  //開(kāi)啟debug模式
$smarty->caching = true;  //開(kāi)啟緩存
$smarty->cache_lifetime = 120; //緩存時(shí)間
$smarty->left_delimiter = '<{';  //左定界符
$smarty->right_delimiter = '}>';  //右定界符
$smarty->template_dir = __DIR__.'/../view/';  //視圖目錄
$smarty->compile_dir = __DIR__ . '/../smarty/compile/';  //編譯目錄
$smarty->config_dir = __DIR__ . '/../smarty/configs/'; //配置目錄
$smarty->cache_dir = __DIR__ . '/../smarty/cache/';  //緩存目錄
$list = range('A', 'D');
$smarty->assign("list", $list);
$smarty->assign("name", "zhezhao");
$smarty->display('index.html');

模板文件index.html

<html>
<head>
  <title></title>
</head>
<body>
  <p><h1><{$name}></h1></p>
  <{foreach $list as $k=>$v }>
    <p><h1><{$k}> : <{$v}></h1></p>
  <{/foreach}>
</body>
</html>

上述方法的優(yōu)點(diǎn)是使用起來(lái)配置比較簡(jiǎn)單,缺點(diǎn)也是顯而易見(jiàn)的,我們controller目錄下可能有很多頁(yè)面調(diào)用smarty模板,在每個(gè)頁(yè)面都需要將上述方法配置一遍。

解決方法有兩種:

將smarty模板的配置信息寫(xiě)到一個(gè)文件中,然后其他頁(yè)面可以通過(guò)包含該文件使用smarty對(duì)象。

require '../smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false;  //開(kāi)啟debug模式
$smarty->caching = true;  //開(kāi)啟緩存
$smarty->cache_lifetime = 120; //緩存時(shí)間
$smarty->left_delimiter = '<{';  //左定界符
$smarty->right_delimiter = '}>';  //右定界符
$smarty->template_dir = __DIR__.'/../view/';  //視圖目錄
$smarty->compile_dir = __DIR__ . '/../smarty/compile/';  //編譯目錄
$smarty->config_dir = __DIR__ . '/../smarty/configs/'; //配置目錄
$smarty->cache_dir = __DIR__ . '/../smarty/cache/';  //緩存目錄

我們自己編寫(xiě)一個(gè)類(lèi),繼承自Smarty類(lèi),然后將配置信息寫(xiě)在構(gòu)造函數(shù)中。

我們編寫(xiě)mySmarty類(lèi)

<?php
require '../smarty/Smarty.class.php';
class mySmarty extends Smarty{
  public function __construct(array $options = array()){
    parent::__construct($options);
    $this->debugging = false; //開(kāi)啟debug模式
    $this->caching = true; //開(kāi)啟緩存
    $this->cache_lifetime = 120;  //緩存時(shí)間
    $this->left_delimiter = '<{'; //左定界符
    $this->right_delimiter = '}>'; //右定界符
    $this->setTemplateDir(__DIR__.'/../view/');  //視圖目錄
    $this->setCompileDir(__DIR__ . '/../smarty/compile/'); //編譯目錄
    $this->setConfigDir(__DIR__ . '/../smarty/configs/'); //配置目錄
    $this->setCacheDir(__DIR__ . '/../smarty/cache/'); //緩存目錄
  }
}

此時(shí),controller里面的index.php代碼可優(yōu)化為:

<?php
require 'mySmarty.php';
$smarty = new mySmarty;
$list = range('A', 'D');
$smarty->assign("list", $list);
$smarty->assign("name", "zhezhao");
$smarty->display('index.html');

最后送上福利:smarty3 chm官方文檔。

更多關(guān)于Smarty相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《smarty模板入門(mén)基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于smarty模板的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論