dd

Yii2 安装smarty3模板引擎及使用

jerry Yii2 2015年11月15日 收藏

github地址:

https://github.com/yiisoft/yii2-smarty

一、安装

推荐通过命令安装:

php composer.phar require --prefer-dist yiisoft/yii2-smarty

composer安装使用使用教程请看PHP 依赖管理工具Composer的使用

要注意composer.phar的位置应该在Yii项目里。

也可以在composer.json里写上要加载的模块

"yiisoft/yii2-smarty": "~2.0.0"

官方提示需要安装subversion,不知道为什么有这种需求。

二、使用

修改web.php配置文件

  return [  
        //....  
        'components' => [  
            'view' => [  
                'renderers' => [  
                    'tpl' => [  
                        'class' => 'yii\smarty\ViewRenderer',  
                        //'cachePath' => '@runtime/Smarty/cache',  
                    ],  
                ],  
            ],  
        ],  
    ];

写controller.php

   public function actionIndex1(){  
            return $this->renderPartial('index1.tpl', ['username' => 'zhangsan']);  
        }

view:index1.tpl

    Hello,{$username}  
    <br />  
    smarty版本:{$smarty.version}  
    <br />  
    1+1={1+1}  
    <br />  
    当前时间:{time()}

Hello,zhansan 

smarty版本:3.1.23 

1+1=2 

当前时间:1432390050


dd