【问题标题】:Using Smarty 3.1 with Kohana 3.3将 Smarty 3.1 与 Kohana 3.3 一起使用
【发布时间】:2014-02-26 05:32:53
【问题描述】:

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

我正在尝试设置 KSmarty,但在使模板正常工作时遇到了困难。这是来自 KSmarty 的“hello world”示例:

application/classes/Controller/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

application/views/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 Views 系统的设计方式,感觉就像是一个杂物,尤其是在 KSmarty 示例之后与 Kohana 对视图的使用相匹配。

我把头发拉出来试图把这个固定下来,考虑到 Kohana 在初始安装时给我的头发拉扯量,这令人印象深刻。我做错了什么?

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

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

【问题讨论】:

    标签: php smarty kohana


    【解决方案1】:

    echo $this-&gt;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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多