[已解决]请问下如何整合smarty和yii?十分感谢

现在我希望smarty负责展示前端页面,yii处理后台数据库中的数据等。

我应该如何整合哦?是在yii的框架之上整合smarty吗?刚刚接触yii和smarty,所以实在是有点没有思路。

十分感谢!

Quote

现在我希望smarty负责展示前端页面,yii处理后台数据库中的数据等。

我应该如何整合哦?是在yii的框架之上整合smarty吗?刚刚接触yii和smarty,所以实在是有点没有思路。

十分感谢!

你可以写一个Smarty Component 其中初始化smarty 设置, 放在 /protected/extensions 文件夹下,然后在配置文件 main.php 文件中的component 数组中加入smarty extension,然后就可以在yii中使用smarty了。

不过,Yii本身的模板和主题模块都设计的十分好,十分灵活,搭配Widget更加powerful ,与Jquery很好的集成,并且十分方便以后重用。php本身就是很好的模板引擎,可以十分高效快捷的生成视图,完全没必要舍近求远用Smarty。

推荐你用用JUI扩展试试,就有感觉了。

Quote

你可以写一个Smarty Component 其中初始化smarty 设置, 放在 /protected/extensions 文件夹下,然后在配置文件 main.php 文件中的component 数组中加入smarty extension,然后就可以在yii中使用smarty了。

不过,Yii本身的模板和主题模块都设计的十分好,十分灵活,搭配Widget更加powerful ,与Jquery很好的集成,并且十分方便以后重用。php本身就是很好的模板引擎,可以十分高效快捷的生成视图,完全没必要舍近求远用Smarty。

推荐你用用JUI扩展试试,就有感觉了。

十分感谢!我去尝试一下

Quote

你可以写一个Smarty Component 其中初始化smarty 设置, 放在 /protected/extensions 文件夹下,然后在配置文件 main.php 文件中的component 数组中加入smarty extension,然后就可以在yii中使用smarty了。

不过,Yii本身的模板和主题模块都设计的十分好,十分灵活,搭配Widget更加powerful ,与Jquery很好的集成,并且十分方便以后重用。php本身就是很好的模板引擎,可以十分高效快捷的生成视图,完全没必要舍近求远用Smarty。

推荐你用用JUI扩展试试,就有感觉了。

请问一下,我在extension中加入了这个文件/smarty/CSmarty.php

内容如下:

      1 <?php

      2 /**

      3  * brief of Config.php:

      4  *

      5  * @author

      6  * @date 2009/04/14 13:34:57

      7  * @version $Revision: 1.1 $

      8  * @todo

      9  */

    10

    11 define("BASEPATH",'dir/test_qxp_0/web');

    12

    13 //smarty setting

    14 //define('TMPDIR', BASEPATH.'/protected/templates/');

    15 define('SMARTY_TMPDIR',BASEPATH.'/protected/templates/');

    16 define('SMARTY_CONFIG',BASEPATH . '/protected/templates/tpl_conf/');

    17 define('SMARTY_CACHEDIR',BASEPATH . '/protected/webdata/tpl_cache/');

    18 define('SMARTY_COMPILE',BASEPATH . '/protected/webdata/tpl_compile/');

    19 define('LIFTTIME',1800);

    20 define('SMARTY_DLEFT', '{/');

    21 define('SMARTY_DRIGHT', '/}');

    22

    23 require_once BASEPATH."/protected/libs/smarty/libs/Smarty.class.php";

    24

    25 class CSmarty extends Smarty{

    26    protected static $_instance = NULL;

    27    static function getInstance(){

    28        if(self::$_instance == NULL){

    29            self::$_instance = new CSmarty();

    30        }

    31        return self::$_instance;

    32    }

    33

    34    function __construct(){

    35        parent::__construct();

    36        $this->template_dir = SMARTY_TMPDIR;

    37        $this->compile_dir = SMARTY_COMPILE;

    38        $this->config_dir = SMARTY_CONFIG;

    39        $this->compile_check = true;

    40        $this->caching = 0;

    41        $this->cache_dir = SMARTY_CACHEDIR;

    42        $this->left_delimiter  =  SMARTY_DLEFT;

    43        $this->right_delimiter =  SMARTY_DRIGHT;

    44        $this->cache_lifetime = LIFTTIME;

    45    }

    46 }

    47

    48 ?>

在main.php中加入了这个

    42                'smarty'=>array(

    43                        'class'=>'application.extensions.smarty.CSmarty',

    44                ),

现在我在controller中这样写:

      1 <?php

      2

      3

      4

      5 class SiteController extends CController

      6 {

      7    //private $smarty;

      8    public function actionIndex()

      9    {

    10        $smarty = CSmarty::getInstance();

    11        $smarty->caching = true;

    12        $smarty->cache_lifetime = 10;

    13        $smarty->display(index.html");

    14    }

为什么会报错哦?错误如下

YiiBase::include(CSmarty.php) [<a href='function.YiiBase-include'>function.YiiBase-include</a>]: failed to open stream: No such file or directory

Quote

不过,Yii本身的模板和主题模块都设计的十分好,十分灵活,搭配Widget更加powerful ,与Jquery很好的集成,并且十分方便以后重用。php本身就是很好的模板引擎,可以十分高效快捷的生成视图,完全没必要舍近求远用Smarty。

推荐你用用JUI扩展试试,就有感觉了。

非常认同!Yii的layout和view的表现层设计非常适合模板化设计。我现在用ZendFramework加smarty,感觉是不错,但如果用Yii的话,我会考虑放弃smarty,因为确实有点舍近求远了。 ;)

既然已经在config 中定义了'smarty',在Controller中应该这样用:

$smarty = Yii::app()->smarty;

Quote

既然已经在config 中定义了'smarty',在Controller中应该这样用:

$smarty = Yii::app()->smarty;

十分感谢will!搞定了!