亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

將Smarty引入到自己項(xiàng)目中,并在自己創(chuàng)建的模板中,將常見的數(shù)據(jù)類型進(jìn)行渲染輸出

Original 2019-04-08 21:22:41 217
abstract://在config目錄下創(chuàng)建config.php進(jìn)行配置 <?php // Smarty 配置文件 // 用Composer安裝的Smarty require __DIR__.'/../vendor/autoload.php'; // 創(chuàng)建Smarty模板引擎對(duì)象 $smarty = new 
//在config目錄下創(chuàng)建config.php進(jìn)行配置
<?php
// Smarty 配置文件

// 用Composer安裝的Smarty
require __DIR__.'/../vendor/autoload.php';

// 創(chuàng)建Smarty模板引擎對(duì)象
$smarty = new Smarty();

// 配置四個(gè)目錄:必選
// 模板文件所在目錄
$smarty->setTemplateDir(__DIR__.'/../temp');

// 模板編譯文件所在目錄
$smarty->setCompileDir(__DIR__.'/../temp_c');

// 緩存目錄
$smarty->setCacheDir(__DIR__.'/../cache');

// 配置目錄
$smarty->setConfigDir(__DIR__.'/../config');
        
// 可選配置
//$smarty->setLeftDelimiter('{');   //變量左定界符
//$smarty->setRightDelimiter('}');  //變量右定界符

// 配置緩存
//$smarty->setCaching(false);    //關(guān)閉緩存
//$smarty->setCacheLifetime(60*60*24); //緩存有效期

//echo '默認(rèn)模板目錄是:'.print_r($smarty->getTemplateDir(),true),'<hr>';
//echo '默認(rèn)模板編譯目錄是:'.$smarty->getCompileDir();

需要?jiǎng)?chuàng)建另外3個(gè)目錄,temp、temp_c、cache,在temp文件下新建demo4.html文件用戶存放模板文件,新建demo4.php

//demo4.php
<?php
//開啟session
session_start();

//加載smarty
require __DIR__.'/config/config.php';

//1.顯示單值變量:標(biāo)量
$name = '小龍女';
//模板賦值
$smarty->assign('name',$name);

//2.數(shù)組:索引數(shù)組
$courses = ['html5','css3','jQuery','php','mysql'];
//模板賦值
$smarty->assign('courses',$courses);

//3.數(shù)組:關(guān)聯(lián)數(shù)組
$book = ['name'=>'php開發(fā)從入門到放棄','price'=>69,'publish'=>'2018-01-22'];
//模板賦值
$smarty->assign('book',$book);

//4.數(shù)組:多維數(shù)組
$books[0] = ['name'=>'php開發(fā)從入門到放棄','price'=>69,'publish'=>'2018-01-22'];
$books[1] = ['name'=>'Mysql性能分析','price'=>99,'publish'=>'2017-02-10'];
$books[2] = ['name'=>'Javascript高級(jí)程序設(shè)計(jì)','price'=>169,'publish'=>'2016-02-22'];
//模板賦值
$smarty->assign('books',$books);

//5.對(duì)象
class Test
{
    public $site = 'php中文網(wǎng)';
    public function welcome()
    {
         return '歡迎來到'.$this->site;
    }
}
$test = new Test();
$smarty -> assign('test',$test);

//6.自定義函數(shù)
function add($a,$b)
{
     return $a+$b;
}

//7.常量,不需要賦值,直接在模板中輸出
const SITE_NAME = 'PHP中文網(wǎng),海量資源';

//8.系統(tǒng)變量,不需要賦值,直接在模板中輸出
$_POST['user_name'] = '超級(jí)管理員';
//$_GET['page'] = 10;
$_SESSION['pass'] = sha1('123456');
//模板渲染
$smarty->display('demo4.html');
//demo4.html

{* 注釋:顯示變量 *}
<h3>我的夢(mèng)中情人:{$name}</h3>
<h3>我的夢(mèng)中情人:{"$name `$books.0.name`"}</h3>

{* 注釋:顯示索引數(shù)組元素 *}
<p>前端課程:{$courses[0]},{$courses[1]},{$courses[2]}</p>
<p>前端課程:{$courses['0']},{$courses['1']},{$courses['2']}</p>
<p>前端課程:{$courses.0},{$courses.1},{$courses.2}</p>

{* 注釋:顯示關(guān)聯(lián)數(shù)組元素 *}
<p>書名:《{$book.name}》,價(jià)格:{$book.price}元,出版時(shí)間:{$book.publish}</p>

{* 注釋:顯示多維數(shù)組元素 *}
<ul>
    <li>書名:《{$books.0.name}》,價(jià)格:{$books.0.price}元,出版時(shí)間:{$books.0.publish}</li>
    <li>書名:《{$books.1.name}》,價(jià)格:{$books.1.price}元,出版時(shí)間:{$books.1.publish}</li>
    <li>書名:《{$books.2.name}》,價(jià)格:{$books.2.price}元,出版時(shí)間:{$books.2.publish}</li>
</ul>

{* 注釋:顯示對(duì)象元素 *}
<p>站點(diǎn)名稱:{$test->site}</p>
<h3>{$test->welcome()}</h3>

{* 訪問自定義函數(shù) *}
<p>求和:{add(5,2)}</p>
<p>求和:{add($books.1.price,5)}</p>

{* 訪問常量 *}
<p>站點(diǎn)常量:{$smarty.const.SITE_NAME}</p>

{* 顯示系統(tǒng)變量 *}
<p>POST提交的用戶名:{$smarty.post.user_name}</p>
<p>GET提交的用戶名:{$smarty.get.page}</p>
<p>SESSION會(huì)話中的密碼:{$smarty.session.pass}</p>

{* 讀取配置文件 *}
{config_load file="app.conf"}
<h3>應(yīng)用名稱是:{$smarty.config.app_name}</h3>
<h3>應(yīng)用名稱是:{$smarty.config.page_title}</h3>

QQ圖片20190408212210.png

Release Notes

Popular Entries