YII2 整合smarty

来源:互联网 发布:充值软件的骗局 编辑:程序博客网 时间:2024/06/18 00:13

最近 在研究YII2这个框架,主要研究了下,依赖注入(DI或者IOC)和事件(AOP),这个框架对于之前做过java编程的人来说无疑是最好的,因为它的风格和Spring结构是一样的设计,本人以前就是从事java开发的,所以接触了这个框架之后很是喜欢,用着很顺手,比CI框架好了很多,TP虽然也是面向对象的,但是跟YII比起来,代码方面还是差了点,YII简洁的代码,用着很舒服,因为YII放弃了对smarty的整合,使用原生的PHP,提高了编译效率,但是,毕竟前端和后端是分开的,使用smarty对前端来说是最好的选择,整合smarty官方建议直接使用composer下载,但是我讨厌这样于是自定义进行了整合:

首先,下载smarty模版百度一下,下载一个吧,这里就不做连接了。

将smarty压缩包解压放到一个自己定义的扩展文件夹,我放在了YII默认的 frontend/libs/smarty(自己创建)下面,然后在libs下面定义一个smarty的子类来扩展smarty为

CSmarty.php

namespace frontend\libs;   use Yii;define("BASEPATH",\Yii::$app->basePath);    require_once \Yii::$app->basePath."/libs/smarty/Smarty.class.php";      define('SMARTY_TMPDIR',BASEPATH.'/views/templates/');//放置模版的目录 //自己创建     define('SMARTY_CACHEDIR',BASEPATH . '/views/template_cache/');//缓存文件目录     define('LIFTTIME',1800);     define('SMARTY_DLEFT', '<{');//左限定符     define('SMARTY_DRIGHT', '}>');//右限定符        class CSmarty extends \Smarty{        protected static $_instance = NULL;        static function getInstance(){            if(self::$_instance == NULL){                self::$_instance = new CSmarty();            }            return self::$_instance;        }            function __construct(){            parent::__construct();            $this->template_dir = SMARTY_TMPDIR;            $this->compile_dir = SMARTY_COMPILE;            $this->config_dir = SMARTY_CONFIG;            $this->compile_check = true;            $this->caching = 1;            $this->cache_dir = SMARTY_CACHEDIR;            $this->left_delimiter  =  SMARTY_DLEFT;            $this->right_delimiter =  SMARTY_DRIGHT;            $this->cache_lifetime = LIFTTIME;                   }        function init(){        }     }

配置frontend/config/main.php

<?php$params = array_merge(    require(__DIR__ . '/../../common/config/params.php'),    require(__DIR__ . '/../../common/config/params-local.php'),    require(__DIR__ . '/params.php'),    require(__DIR__ . '/params-local.php'));//配置自己需要的组建return [    'id' => 'app-frontend',    'basePath' => dirname(__DIR__),    'bootstrap' => ['log'],    'controllerNamespace' => 'frontend\controllers',    'components' => [        'user' => [            'identityClass' => 'common\models\User',            'enableAutoLogin' => true,        ],        'log' => [            'traceLevel' => YII_DEBUG ? 3 : 0,            'targets' => [                [                    'class' => 'yii\log\FileTarget',                    'levels' => ['error', 'warning'],                ],            ],        ],        'errorHandler' => [            'errorAction' => 'site/error',        ],        'cache'=>[                'class' =>'yii\\caching\\FileCache'        ],        'mailer'=>[            'mailer' =>'yii\\mail\\BaserMailer'        ],         'urlManager' => [            'class' => 'yii\web\urlManager',            'enablePrettyUrl' => true,        ],        'smarty'=>[                    'class'=>'frontend\libs\CSmarty'//将自己定义CSmarty的命名空间放在这里        ],    ],    'params' => $params,];

最后一步就可以引用smarty了

 public function actionTest(){            $smarty = \Yii::$app->smarty;       $smarty->assign('name','HelloSmarty');      $smarty->display("index.html");}
访问自己的控制器就会跳转到自己定义的模版了:


以上是没有进行第三方的扩展进行使用的smarty,如果想直接使用第三方扩展请参考:https://github.com/gaoxuxu123/yii-smarty

1 0