Kohana template $content variable shows nothing

心已入冬 提交于 2019-12-25 05:36:12

问题


I have a Kohana controller that extends Kohana template class. The Kohana template class has

const CONTENT_KEY = 'content';

In this controller I have declared the template view to be used:

public $template = 'homepage_template';

Also in this controller I have some methods, and some associated views.

Having all these, when I echo $content in the homepage_template view, nothing shows (no content from the views that belong to the actions from this controller). (I have auto_render true in the actions). What can be the cause for that?


回答1:


Thats how I use Template Controller, hope it will help:

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

class Controller_Template_Default extends Controller_Template
{
    public $template = 'templates/default';

    protected $auth;

    protected $user;

    public function before()
    {
        parent::before();

        Session::instance();

        $this->auth = Auth::instance();     

        if (($this->user = $this->auth->get_user()) === FALSE)
        {
            $this->user = ORM::factory('user');
        }

        if($this->auto_render)
        {
            $this->template->title            = '';
            $this->template->meta_keywords    = '';
            $this->template->meta_description = '';
            $this->template->meta_copywrite   = '';
            $this->template->header           = '';
            $this->template->content          = '';
            $this->template->footer           = '';
            $this->template->styles           = array();
            $this->template->scripts          = array();
            $this->template->bind_global('user', $this->user);
        }
    }

    public function after()
    {
        if($this->auto_render)
        {
                $styles                  = array('css/default.css' => 'screen', 'css/reset.css' => 'screen');
                $scripts                 = array('js/infieldlabel.js', 'js/menu.js', 'js/jquery.js');

                $this->template->styles  = array_reverse(array_merge($this->template->styles, $styles));
                $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts));
            }

        parent::after();
    }
} // End Controller_Template_Default

P.S. you shouldn't use constant for content. Extend your controller from Controller_TEemplate_Default and bind it like $this->template->content = View::factory('path to the view');



来源:https://stackoverflow.com/questions/9034867/kohana-template-content-variable-shows-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!