内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何正確的使用smarty模板-創(chuàng)新互聯(lián)

本篇文章為大家展示了如何正確的使用smarty模板,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司是少有的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、營(yíng)銷型企業(yè)網(wǎng)站、微信小程序開發(fā)、手機(jī)APP,開發(fā)、制作、設(shè)計(jì)、買鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,公司2013年成立,堅(jiān)持透明化,價(jià)格低,無套路經(jīng)營(yíng)理念。讓網(wǎng)頁驚喜每一位訪客多年來深受用戶好評(píng)

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


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

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

如何正確的使用smarty模板

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

index.php

<?php
require '../smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false;  //開啟debug模式
$smarty->caching = true;  //開啟緩存
$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><h2><{$name}></h2></p>
  <{foreach $list as $k=>$v }>
    <p><h2><{$k}> : <{$v}></h2></p>
  <{/foreach}>
</body>
</html>

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

解決方法有兩種:

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

require '../smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false;  //開啟debug模式
$smarty->caching = true;  //開啟緩存
$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/';  //緩存目錄

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

我們編寫mySmarty類

<?php
require '../smarty/Smarty.class.php';
class mySmarty extends Smarty{
  public function __construct(array $options = array()){
    parent::__construct($options);
    $this->debugging = false; //開啟debug模式
    $this->caching = true; //開啟緩存
    $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');

上述內(nèi)容就是如何正確的使用smarty模板,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站題目:如何正確的使用smarty模板-創(chuàng)新互聯(lián)
文章地址:http://www.rwnh.cn/article44/idghe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)定制網(wǎng)站、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
巫山县| 巢湖市| 盘锦市| 巴塘县| 河源市| 吉林市| 崇义县| 肇州县| 鹿泉市| 新田县| 黔南| 天峻县| 潼南县| 呼玛县| 金塔县| 凤台县| 盐城市| 黄梅县| 枞阳县| 灵宝市| 蒙山县| 闽清县| 始兴县| 紫阳县| 永修县| 长治县| 迁安市| 洪江市| 福清市| 南江县| 宁津县| 宿松县| 吴川市| 徐水县| 化德县| 墨竹工卡县| 孙吴县| 福州市| 武宁县| 弥勒县| 麻城市|