smarty基礎應用源碼

index.php

<?php
/*	//直接在本文件中配置
//庫文件包含
define('SMARTY_DIR','D:/APMServ5.2.6/www/htdocs/smartydemo/smarty/');
require(SMARTY_DIR.'smarty.class.php');
//配置
$smarty = new Smarty;
$smarty->compile_dir = SMARTY_DIR.'templates_c/';
$smarty->cache_dir = AMARTY_DIR.'cache/';
$smarty->template_dir = 'templates/';
$smarty->config_dir = 'config/';

$this->caching = true;
//變量傳值
$smarty->assign('name', 'phf');
//顯示模板文件
$smarty->display('index.tpl');
//$smarty->display('index.tpl', 2);	//多份緩存
*/
/* //取出生成的文件
$file = $smarty->fetch('index.tpl');
echo $file;
*/
/*
//單個文件的cache時間設置
$smarty->caching = 2; // lifetime is per cache
$smarty->cache_lifetime = 300;
$smarty->display('index.tpl');
*/

// 實初化配置的進一步類封裝
require_once('smartydemo.class.php');
$smarty = new Smarty_demo;
$smarty->assign('name', 'phf');
//$smarty->display('index.tpl');
$smarty->display('index.tpl', 2);

smartydemo.class.php

<?php
//
define('SMARTY_DIR','D:/APMServ5.2.6/www/htdocs/smartydemo/smarty/');
require(SMARTY_DIR.'smarty.class.php');

class Smarty_demo  extends Smarty
{
	function __construct()
	{
		Smarty::__construct();
		
		$this->compile_dir = SMARTY_DIR.'templates_c/';
		$this->cache_dir = SMARTY_DIR.'cache/';
		$this->template_dir = 'templates/';
		$this->config_dir = 'config/';
		
		$this->caching = true;
		$this->cache_lifetime = 1;
		$this->assign('app_name','smartydemo');		
	}
}

index.tpl

{* smarty *}
<h1>{$app_name}</h1>

hello {$name}!;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章