2014-02-26 53 views
0

KSmarty是一个Kohana模块,旨在将Smarty与Kohana集成。我试图将我目前的项目(已经使用Smarty)迁移到使用Kohana。使用Smarty 3.1与Kohana 3.3

我试图让KSmarty成立,但我很难让模板正常工作。这是在 “世界你好” 例如从KSmarty:

应用/类/控制器/的welcome.php

<?php defined('SYSPATH') or die('No direct script access.'); 

class Controller_Welcome extends Controller_Template 
{ 
    public $template = 'welcome'; 

    public function action_index() 
    { 
     // Assign a value to the variable 'intro' 
     $this->template->intro = 'Hello world!'; 

     // Create a nested view by loading a different template 
     $this->template->content = View::factory('content'); 
    } 
} 
// End Welcome 

应用/视图/ welcome.tpl

<html> 
    <body> 
     <h1>{$intro}</h1> 
     <p> 
      {$content} 
     </p> 
    </body> 
</html> 

application/views/content.tpl

Yes, this works! 

但是,对于我来说,控制器/视图组合无法按预期方式工作。下面是我试过的action_index()的变体:

public function action_index() 
{ 
    echo 'foo'; 
} 
// Output: foo 

public function action_index() 
{ 
    // Assign a value to the variable 'intro' 
    $this->template->intro = 'Hello world!'; 

    // Create a nested view by loading a different template 
    $this->template->content = View::factory('content'); 
} 
// No output 
// No error in apache log, php log, or kohana log 

public function action_index() 
{ 
    Ksmarty::instance()->assign(array(
     'intro' => 'Hello world!', 
     'content' => APPPATH.'/views/content.tpl' 
     // Note: also changed {$content} in template to {include $content} 
    )); 
    Ksmarty::instance()->display(APPPATH.'/views/welcome.tpl'); 
} 
// Expected HTML output 

可以只需使用Ksmarty::instance()这样,让我的网站的工作,但这不是Kohana的查看系统是如何设计的,而且感觉就像一个kludge,特别是因为KSmarty的例子与Kohana使用Views相匹配。

我拉我的头发试图把这一个下来,这是令人印象深刻的考虑到头发拉动Kohana在初始安装给我的数量。我究竟做错了什么?

哦,我做两个更改KSmarty达到这一点:

  1. Kohana::$config->load('smarty')取代的Kohana::config('smarty')所有实例;据我所知,这是Kohana版本变化的问题。
  2. 已注释$s->security = Kohana::$config->load('smarty')->security;;据我所知,这是Smarty版本更改的问题,而KSmarty无论如何都配置为FALSE

回答

0

echo $this->template;添加到视图的结尾。这不在Kohana和KSmarty文档/示例中,但它足够满足我。如果其他人提出解决问题的答案,而不是echo,我会将答案标记为已接受,但在此之前,我有一个解决方案。

<?php defined('SYSPATH') or die('No direct script access'); 

class Controller_Welcome extends Controller_Template 
{ 
    public $template = 'welcome'; 

    public function action_index() 
    { 
     // Assign a value to the variable 'intro' 
     $this->template->intro = 'Hello world!'; 

     // Create a nested view by loading a different template 
     $this->template->content = View::factory('content'); 

     // Output the view 
     echo $this->template; 
    } 
} 
// End Welcome