简体   繁体   中英

Using Smarty 3.1 with Kohana 3.3

KSmarty is a Kohana module meant to integrate Smarty with Kohana. I'm trying to migrate my current project (already using Smarty) to using Kohana.

I'm trying to get KSmarty set up, but I'm having difficulties getting the templates to work. This is the "hello world" example from KSmarty:

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!

However, for me, the controller/view combo does not work as expected. Here are the variants of action_index() that I've tried:

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

I could simply use Ksmarty::instance() like this and get my website working, but this isn't how the Kohana Views system was designed, and it feels like a kludge, especially since the KSmarty example matches up with Kohana's use of Views.

I'm pulling my hair out trying to pin this one down, which is impressive considering the amount of hair-pulling Kohana gave me on the initial install. What am I doing wrong?

Oh, I have make two changes to KSmarty to reach this point:

  1. All instances of Kohana::config('smarty') replaced with Kohana::$config->load('smarty') ; as far as I can tell, this is a matter of a version change in Kohana.
  2. Commented out $s->security = Kohana::$config->load('smarty')->security; ; as far as I can tell, this is a matter of a version change in Smarty, and KSmarty is configured to FALSE anyway.

Adding echo $this->template; to the end of the view works. It's not in the Kohana nor the KSmarty documentation/examples, but it's close enough to satisfy me. If anyone else ever comes up with an answer that solves the problem without echo , I'll mark that answer as accepted, but until that time, I have a solution.

<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM