Yii2 安装smarty3模板引擎及使用

jerry Yii2 2015年11月15日 收藏

github地址:

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

一、安装

推荐通过命令安装:

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

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

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

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

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

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

二、使用

修改web.php配置文件

  1.   return [  
  2.         //....  
  3.         'components' => [  
  4.             'view' => [  
  5.                 'renderers' => [  
  6.                     'tpl' => [  
  7.                         'class' => 'yii\smarty\ViewRenderer',  
  8.                         //'cachePath' => '@runtime/Smarty/cache',  
  9.                     ],  
  10.                 ],  
  11.             ],  
  12.         ],  
  13.     ];

写controller.php

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

view:index1.tpl

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

Hello,zhansan 

smarty版本:3.1.23 

1+1=2 

当前时间:1432390050