以插件形式加入smarty.
根据CI的规则。
下载smarty,并将其放到application/libraries/smarty下。
新建Cismarty.php,放在application/libraries下
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
- <?php defined('BASEPATH') or die('Access restricted!');
-
-
-
- require(APPPATH.'libraries/smarty/Smarty.class.php');
- class Cismarty extends Smarty
- {
- public $ext = 'tpl';
- public $dir = '';
- public $layout = 'layout/main';
-
-
-
-
-
-
-
- public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '')
- {
- $this->Smarty();
- if(is_array($template_dir)){
- foreach ($template_dir as $key => $value) {
- $this->$key = $value;
- }
- }
- else {
- $this->cache_dir = $cache_dir ? $cache_dir : BASEPATH . 'cache/';
- $this->template_dir = $template_dir ? $template_dir : APPPATH . 'views/';
- $this->compile_dir = $compile_dir ? $compile_dir : APPPATH . 'tpl_c/';
- $this->compile_check = true;
- $this->debugging = false;
- $this->caching = 0;
- $this->cache_lefetime= 6000;
- $this->left_delimiter= '<!--{';
- $this->right_delimiter= '}-->';
- }
- }
-
-
-
-
-
-
- public function show($tpl){
- $this->assign('jsFiles',$this->getJsHtml());
- $this->assign('jsFiles1',$this->getJsHtml(1));
- $this->assign('LAYOUT', $this->dir ? $this->dir.'/'.$tpl.'.'.$this->ext : $tpl.'.'.$this->ext);
- $this->display($this->layout.'.'.$this->ext);
- }
-
-
-
-
-
-
- public function addCss($file) {
- if (strpos($file,'/')==false) {
- $file = config_item('css') . $file;
- }
- $GLOBALS['cssFiles'][$file] = $file;
- }
-
-
-
-
-
-
- public function addJs($file,$btm=NULL) {
- if (strpos($file,'/')==false) {
- $file = config_item('js') . $file;
- }
- if ($btm==NULL) {
- $GLOBALS['jsfiles'][$file] = $file;
- } else {
- $GLOBALS['jsbtmfiles'][$file] = $file;
- }
- }
-
-
-
-
-
- public function getJsHtml($btm=NULL) {
- $html = '';
- if (!$GLOBALS['jsfiles']) {
- return;
- }
- $jsFile = $btm?'jsbtmfiles':'jsfiles';
- if (@$GLOBALS[$jsFile]) {
- foreach ($GLOBALS[$jsFile] as $value) {
- $html .= $this->jsInclude($value,true)."/n";
- }
- return $html;
- } else {
- return ;
- }
- }
-
-
-
-
-
-
-
- public function addTag($tag, $attribute = NULL, $content = NULL) {
- $this->js();
- $html = '';
- $tag = strtolower($tag);
- $html .= '<'.$tag;
- if ($attribute!=NULL) {
- if (is_array($attribute)) {
- foreach ($attribute as $key=>$value) {
- $html .= ' '.strtolower($key).'="'.$value.'"';
- }
- } else {
- $html .= ' '.$attribute;
- }
- }
- if ($content) {
- $html .= '>'.$content.'</'.$tag.'>';
- } else {
- $html .= ' />';
- }
- $this->output .= $html;
- return $html;
- }
-
-
-
-
-
- public function addText($content) {
- $this->js();
- $content = htmlentities($content);
- $this->output .= $content;
- return $content;
- }
-
-
-
-
-
-
- public function js($jscode = NULL, $end = false) {
- if (!$this->inJsArea && $jscode) {
- $this->output .= "/n<mce:script language='JavaScript' type='text/javascript'><!--
- /n
- $this->inJsArea = true;
- }
- if ($jscode==NULL && $this->inJsArea==true) {
- $this->output .= "/n
-
- $this->inJsArea = false;
- } else {
- $this->output .= "/t$jscode/n";
- if ($end) {
- $this->js();
- }
- }
- return;
- }
-
-
-
-
-
-
- public function jsAlert($message, $end = false) {
- $this->js('alert("' . strtr($message, '"', '//"') . '");', $end);
- }
-
-
-
-
-
-
- public function jsInclude($fileName,$return = false, $defer = false) {
- if (!$return) {
- $this->js();
- }
- $html = '<mce:script language="JavaScript" type="text/javascript" src="'
- . $fileName . '" mce_src="'
- . $fileName . '"' . ( ($defer) ? ' defer' : '' )
- . '></mce:script>';
- if (!$return) {
- $this->output .= $html;
- } else {
- return $html;
- }
- }
-
-
-
-
-
- public function cssInclude($fileName,$return = false) {
- if (!$return) {
- $this->js();
- }
- $html = '<LINK href="' . $fileName . '" mce_href="' . $fileName . '" rel=stylesheet>' . chr(13);
- if (!$return) {
- $this->output .= $html;
- } else {
- return $html;
- }
- }
-
-
-
-
-
- public function output($print = false) {
- $this->js();
- if ($print) {
- echo $this->output;
- $this->output = '';
- return;
- } else {
- $output = $this->output;
- $this->output = '';
- return $output;
- }
- }
- }
- ?>
打开配置文件(config/autoload.php)
$autoload['libraries'] = array('cismarty');//加入cismarty
这样在所有应用中,都可以使用$this->cismarty来调用smarty实例了。
Cismarty类已经在原有的smarty::display基础上做了小的改动,已经支持layout功能
默认加载view/layout/main.tpl作为布局文件。
可以通过$this->cismarty->layout = "xxxx"来更改
http://blog.csdn.net/a600423444/article/details/5892726
分享到:
相关推荐
8. **模板继承和布局**:Smarty提供了一种布局(layout)的概念,可以创建一个基础布局模板,其他模板可以引用这个布局,并在其内部添加自己的内容。 9. **预处理和后处理**:Smarty允许在模板处理前或处理后执行...
3. **Smarty安装与配置**:如何下载、安装Smarty,并进行基本的配置,包括设置模板目录、编译目录等。 4. **Smarty语法**:介绍Smarty的模板语法,如变量插值、控制结构(if/else、foreach等)、函数和修饰器的使用...
- PHP框架和模板引擎:虽然这里没有提及具体的框架,但了解常见的PHP框架(如Laravel、CodeIgniter)和模板引擎(如Twig、Smarty)将有助于理解整个网站的工作原理。 - 网站配置:学习如何解析并修改"conf.php",以...
8. **STP Simple Template Parser**:此模板解析类允许在多个模板中组合页面,并将结果输出到浏览器或文件系统。 9. **OO Template Class**:面向对象的模板类,提供了一种结构化的模板处理方式。 10. **Simple...
在IT行业中,构建一个高效、美观的企业网站是展示企业形象、提供服务的重要途径。"huagong-100820-a17_企业网站模板PHP整站源码"是一个用于创建...理解并运用这些知识点,开发者可以轻松地构建和定制自己的企业网站。