`

CodeIgniter支持smarty,并配置支持layout

阅读更多

以插件形式加入smarty.

根据CI的规则。

下载smarty,并将其放到application/libraries/smarty下。

新建Cismarty.php,放在application/libraries下

 

[php:showcolumns:firstline[1]] view plaincopy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <?php defined('BASEPATH'or die('Access restricted!');  
  2. /* 
  3.  * 作者:晋勇 
  4.  */  
  5. require(APPPATH.'libraries/smarty/Smarty.class.php');  
  6. class Cismarty extends Smarty  
  7. {  
  8.     public $ext = 'tpl';  
  9.     public $dir = '';  
  10.     public $layout = 'layout/main';  
  11. /** 
  12.   * 构造函数 
  13.   * 
  14.   * @access public 
  15.   * @param array/string $template_dir 
  16.   * @return obj  smarty obj 
  17. */  
  18.     public function __construct($template_dir = ''$compile_dir = ''$config_dir = ''$cache_dir = '')  
  19.     {  
  20.          $this->Smarty();  
  21.          if(is_array($template_dir)){  
  22.           foreach ($template_dir as $key => $value) {  
  23.            $this->$key = $value;  
  24.           }  
  25.          }  
  26.          else {  
  27.             $this->cache_dir    = $cache_dir    ? $cache_dir    : BASEPATH . 'cache/';  
  28.             $this->template_dir  =   $template_dir ? $template_dir : APPPATH . 'views/';  
  29.             $this->compile_dir   =   $compile_dir  ? $compile_dir  : APPPATH . 'tpl_c/';  
  30.             $this->compile_check =   true;  
  31.             $this->debugging     =   false;  //debug模式  
  32.             $this->caching       =   0;  //启用缓存  
  33.             $this->cache_lefetime=   6000;//缓存时间s  
  34.             $this->left_delimiter=   '<!--{';  
  35.             $this->right_delimiter=  '}-->';  
  36.          }  
  37.     }  
  38.       
  39.     /** 
  40.      * 显示输出页面 
  41.      * @access public 
  42.      * @return string 
  43.      */  
  44.     public function show($tpl){  
  45.         $this->assign('jsFiles',$this->getJsHtml());  
  46.         $this->assign('jsFiles1',$this->getJsHtml(1));  
  47.         $this->assign('LAYOUT'$this->dir ? $this->dir.'/'.$tpl.'.'.$this->ext : $tpl.'.'.$this->ext);  
  48.         $this->display($this->layout.'.'.$this->ext);  
  49.     }  
  50.     /** 
  51.      * 添加一个CSS文件包含 
  52.      * @param string $file 文件名 
  53.      * @access public 
  54.      * @return void 
  55.      */  
  56.     public function addCss($file) {  
  57.         if (strpos($file,'/')==false) {  
  58.             $file = config_item('css') . $file;  
  59.         }  
  60.         $GLOBALS['cssFiles'][$file] = $file;  
  61.     }  
  62.     /** 
  63.      * 添加一个JS文件包含 
  64.      * @param string $file 文件名 
  65.      * @access public 
  66.      * @return void 
  67.      */  
  68.     public function addJs($file,$btm=NULL) {  
  69.         if (strpos($file,'/')==false) {  
  70.             $file = config_item('js') . $file;  
  71.         }  
  72.         if ($btm==NULL) {  
  73.             $GLOBALS['jsfiles'][$file] = $file;  
  74.         } else {  
  75.             $GLOBALS['jsbtmfiles'][$file] = $file;  
  76.         }  
  77.     }  
  78.     /** 
  79.      * 取生成的包含JS HTML 
  80.      * @access public 
  81.      * @return string 
  82.      */  
  83.     public function getJsHtml($btm=NULL) {  
  84.         $html = '';  
  85.         if (!$GLOBALS['jsfiles']) {  
  86.             return;  
  87.         }  
  88.         $jsFile = $btm?'jsbtmfiles':'jsfiles';  
  89.         if (@$GLOBALS[$jsFile]) {  
  90.             foreach ($GLOBALS[$jsFileas $value) {  
  91.                 $html .= $this->jsInclude($value,true)."/n";  
  92.             }  
  93.             return $html;  
  94.         } else {  
  95.             return ;  
  96.         }  
  97.     }  
  98.     /** 
  99.      * 添加html标签 
  100.      * @param string $tag 标签名 
  101.      * @param mixed $attribute 属性 
  102.      * @param string $content 内容 
  103.      * @return string 
  104.      */  
  105.     public function addTag($tag$attribute = NULL, $content = NULL) {  
  106.         $this->js();  
  107.         $html = '';  
  108.         $tag = strtolower($tag);  
  109.         $html .= '<'.$tag;  
  110.         if ($attribute!=NULL) {  
  111.             if (is_array($attribute)) {  
  112.                 foreach ($attribute as $key=>$value) {  
  113.                     $html .= ' '.strtolower($key).'="'.$value.'"';  
  114.                 }  
  115.             } else {  
  116.                 $html .= ' '.$attribute;  
  117.             }  
  118.         }  
  119.         if ($content) {  
  120.             $html .= '>'.$content.'</'.$tag.'>';  
  121.         } else {  
  122.             $html .= ' />';  
  123.         }  
  124.         $this->output .= $html;  
  125.         return $html;  
  126.     }  
  127.     /** 
  128.      * 添加html文本 
  129.      * @param string $content 内容 
  130.      * @return string 
  131.      */  
  132.     public function addText($content) {  
  133.         $this->js();  
  134.         $content = htmlentities($content);  
  135.         $this->output .= $content;  
  136.         return $content;  
  137.     }  
  138.     /** 
  139.      * 添加js代码 
  140.      * @param string $jscode js代码 
  141.      * @param bool $end 是否关闭js 代码块 
  142.      * @return void 
  143.      */  
  144.     public function js($jscode = NULL, $end = false) {  
  145.         if (!$this->inJsArea && $jscode) {  
  146.             $this->output .= "/n<mce:script language='JavaScript' type='text/javascript'><!--  
  147. /n//<!--[CDATA[/n";  
  148.             $this->inJsArea = true;  
  149.         }  
  150.         if ($jscode==NULL && $this->inJsArea==true) {  
  151.             $this->output .= "/n//]]-->/n  
  152. // --></mce:script>/n";  
  153.             $this->inJsArea = false;  
  154.         } else {  
  155.             $this->output .= "/t$jscode/n";  
  156.             if ($end) {  
  157.                 $this->js();  
  158.             }  
  159.         }  
  160.         return;  
  161.     }  
  162.     /** 
  163.      * 添加js提示代码 
  164.      * @param string $message 提示内容 
  165.      * @param bool $end 是否关闭js 代码块 
  166.      * @return void 
  167.      */  
  168.     public function jsAlert($message$end = false) {  
  169.         $this->js('alert("' . strtr($message'"''//"') . '");'$end);  
  170.     }  
  171.     /** 
  172.      * 添加js文件包含 
  173.      * @param string $fileName 文件名 
  174.      * @param bool $defer 是否添加defer标记 
  175.      * @return string 
  176.      */  
  177.     public function jsInclude($fileName,$return = false, $defer = false) {  
  178.         if (!$return) {  
  179.             $this->js();  
  180.         }  
  181.         $html = '<mce:script language="JavaScript" type="text/javascript" src="'  
  182.                 . $fileName . '" mce_src="'  
  183.                 . $fileName . '"' . ( ($defer) ? ' defer' : '' )  
  184.                 . '></mce:script>';  
  185.         if (!$return) {  
  186.             $this->output .= $html;  
  187.         } else {  
  188.             return $html;  
  189.         }  
  190.     }  
  191.     /** 
  192.      * 添加css文件包含 
  193.      * @param string $fileName 文件名 
  194.      * @return string 
  195.      */  
  196.     public function cssInclude($fileName,$return = false) {  
  197.         if (!$return) {  
  198.             $this->js();  
  199.         }  
  200.         $html = '<LINK href="' . $fileName . '" mce_href="' . $fileName . '" rel=stylesheet>' . chr(13);  
  201.         if (!$return) {  
  202.             $this->output .= $html;  
  203.         } else {  
  204.             return $html;  
  205.         }  
  206.     }  
  207.     /** 
  208.      * 输出html内容 
  209.      * @param bool $print 是否直接输出,可选,默认返回 
  210.      * @return void 
  211.      */  
  212.     public function output($print = false) {  
  213.         $this->js();  
  214.         if ($print) {  
  215.             echo $this->output;  
  216.             $this->output = '';  
  217.             return;  
  218.         } else {  
  219.             $output = $this->output;  
  220.             $this->output = '';  
  221.             return $output;  
  222.         }  
  223.     }  
  224. }  
  225. ?>  
 

 

打开配置文件(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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics