开发者

How to render a view in Kohana

开发者 https://www.devze.com 2023-02-26 13:39 出处:网络
I just started using Kohana and I\'m having some trouble rendering a simple view. I created the following controller:

I just started using Kohana and I'm having some trouble rendering a simple view. I created the following controller:

class Controller_Tracer extends Controller {

    public function action_index() {        
        $this->request->response = View::factory('tracer');
    }

}

Then I've created this view in application/views/tracer.php:

Tracer开发者_开发技巧 view

Now when I try to access the controller via http://mydomain/index.php/tracer/index it's just displaying a blank page. It seems to be finding both the controller and view since when I change the names there are errors but it's just not displaying anything.

Does anybody know what could be the issue?


In Kohana 3.1 you would instead use:

$this->response->body(View::factory('tracer'));

Have a quick look over the docs for migrating from 3.0 to 3.1.


Besides davgothic's solution, you also can use Controller_Template. Using Controller_Template make it easier to manage template & content

class Controller_Tracer extends Controller {

    public $template = 'yourtemplatefile'; // HTML template inside views folder

    public function before() {

        parent::before();
        $this->template->title = 'My Website';

    }

    public function action_index() {        

        $this->template->content = 'Hello World';
    }

    public function action_trace() {
        $this->template->content = View::factory('tracer');
    }
}

Inside views/yourtemplatefile.php:

<html>
    <head>
    <title><?php echo isset($title) ? $title : ''; ?></title>
    </head>

    <body>

       <h1><?php echo isset($title) ? $title : ''; ?></h1>

       <?php echo isset($content) ? $content : ''; ?>

    </body>
</html>

Inside views/tracer.php:

<p>This is tracer.</p>

<p>Nulla vitae elit libero, a pharetra augue.</p>

If you try to access http://mydomain/index.php/tracer/index, you will get:

My Website

Hello World

If you try to access http://mydomain/index.php/tracer/trace, you will get:

My Website

This is tracer.

Nulla vitae elit libero, a pharetra augue.

Hope that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消