1个例子搞定smarty配置
发布于 2022年 01月 21日 05:16
- define('ROOT_PATH', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
- define('TEM_PATH', ROOT_PATH ."smartytest". DIRECTORY_SEPARATOR);
- $_path = array(
- '.',
- ROOT_PATH .DIRECTORY_SEPARATOR."smartytest".DIRECTORY_SEPARATOR.'smarty'.DIRECTORY_SEPARATOR.'libs',
- get_include_path(),
- );
- set_include_path(implode(PATH_SEPARATOR, $_path));
- ?>
- session_start();
- require('Smarty.class.php');
- class Controller
- {
- public $tpl = NULL;
- public function __construct() {
- $this->tpl = new Smarty();
- $this->tpl->debugging = false;
- $this->tpl->compile_check = true;
- $this->tpl->force_compile = false;
- $this->tpl->cache_dir = TEM_PATH ."templates_cache";
- $this->tpl->template_dir = TEM_PATH . "templates";
- $this->tpl->compile_dir = TEM_PATH . "templates_c";
- $this->tpl->cache = false;
- }
- public function addCss($css) {
- if (!is_array($css)) {
- $this->tpl->append('cssArr', $css);
- } else {
- $this->tpl->append('cssArr', $css, true);
- }
- }
- public function addJs($js) {
- if (!is_array($js)) {
- $this->tpl->append('jsArr', $js);
- } else {
- $this->tpl->append('jsArr', $js, true);
- }
- }
- }
- ?>
tpl = new Smarty(); $this->tpl->debugging = false; $this->tpl->compile_check = true; $this->tpl->force_compile = false; $this->tpl->cache_dir = TEM_PATH ."templates_cache"; $this->tpl->template_dir = TEM_PATH . "templates"; $this->tpl->compile_dir = TEM_PATH . "templates_c"; $this->tpl->cache = false; } public function addCss($css) { if (!is_array($css)) { $this->tpl->append('cssArr', $css); } else { $this->tpl->append('cssArr', $css, true); } } public function addJs($js) { if (!is_array($js)) { $this->tpl->append('jsArr', $js); } else { $this->tpl->append('jsArr', $js, true); } }}?>
- require_once 'init.php';
- require_once 'control.php';
- class indexController extends Controller {
- private $templateName = 'index.tpl';
- private $test = array("安徽","合肥","六安","苏埠");
- public function __construct(){
- parent::__construct();
- }
- //页面入口程序
- public function indexAction(){
- $this->tpl->assign("checkbox",$this->test);
- }
- //模板渲染
- public function showTemplate(){
- $this->tpl->display($this->templateName);
- }
- }
- $objIndex = new indexController();
- $objIndex->indexAction();
- $objIndex->showTemplate();
- ?>
tpl->assign("checkbox",$this->test); } //模板渲染 public function showTemplate(){ $this->tpl->display($this->templateName); }}$objIndex = new indexController();$objIndex->indexAction();$objIndex->showTemplate();?>