CI框架下smarty3的整合步骤(附代码)
本篇文章给大家带来的内容是关于CI框架下smarty3的整合步骤(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1 下载smarty3并将libs文件放在框架libraries目录下重命名为smarty
2 在libraries下创建Ci_smarty.php文件,代码如下
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once(APPPATH.'libraries/smarty/Smarty.class.php'); //这里指定Smarty.class.php的存放位置 class Ci_smarty extends Smarty { protected $ci; public function __construct() { parent::__construct(); $this->ci = & get_instance(); $this->ci->load->config('smarty');//加载smarty的配置文件 $this->cache_lifetime =$this->ci->config->item('cache_lifetime'); $this->caching = $this->ci->config->item('caching'); $this->config_dir = $this->ci->config->item('config_dir'); $this->template_dir = $this->ci->config->item('template_dir'); $this->compile_dir = $this->ci->config->item('compile_dir'); $this->cache_dir = $this->ci->config->item('cache_dir'); $this->use_sub_dirs = $this->ci->config->item('use_sub_dirs'); $this->left_delimiter = $this->ci->config->item('left_delimiter'); $this->right_delimiter = $this->ci->config->item('right_delimiter'); } }
3 在框架config目录下创建smarty.php,代码如下
<?php $config['cache_lifetime'] = 3600;//缓存失效 $config['caching'] = true;//开启缓存 $config['template_dir'] = APPPATH .'views'; $config['compile_dir'] = APPPATH .'views/template_c'; $config['cache_dir'] = APPPATH . 'views/cache'; $config['config_dir'] = APPPATH . 'views/config'; $config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录) $config['left_delimiter'] = '{'; $config['right_delimiter'] = '}';
4 在配置文件autoload.php自动加载ci_smarty
$autoload['libraries']=array('ci_smarty');
5 在框架的扩展父类MY_Controller.php(没有就现在core下创建)添加如下代码
/ * @param $key * @par * smarty assign */ public function assign($key,$val){ $this->cismarty->assign($key,$val); } /** * @param $html * smarty smarty display方法 */ public function display($html,$is_cache=false){ if(!$is_cache) { $this->ci_smarty->clearCache($html); } $this->ci_smarty->display($html);} /** * smarty清除所有缓存 * @author shangshikai */ public function clearAllCache(){ $this->ci_smarty->clearAllCache(); } /** * smarty 清除某个模板的缓存 * @author shangshikai */ public function clearCache($html){ $this->ci_smarty->clearCache($html); }
/** * @param $html * @return mixed * smarty判断该模板是否有缓存 */ public function isCached($html) { return $this->ci_smarty->isCached($html); }
6 由于在配置文件smarty.php中开启缓存,但不是所有页面都适合缓存,所以在MY_Controller中配置display方法时应增加参数默认清除缓存,需要缓存的页面只需在调用display方法时传递第二个参数为true。在使用缓存后,如果需要局部不需要缓存可以使用{nocache}{/nocache}标签包裹,如果标签不缓存使用方法是在标签后增加nocache 如{foreach $arr as $v nocache}
7 如果整个项目都不使用缓存,可以在smarty.php中去掉$config['cache_lifetime'] = 3600;$config['caching'] = true;两行,并且在MY_Controller中的display方法去掉第二个参数以及相关判断
以上是CI框架下smarty3的整合步骤(附代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸
