Kohana - subfolders within views folder
I'm working on the admin section of a site using Kohana. I've created a "admin" subfolder within the views folder to store admin views. I'm also u开发者_StackOverflow中文版sing a modified instance of the Template Controller for the admin section called Admin Template Controller, seen here:
abstract class Admin_Template_Controller extends Template_Controller
{
public $template = 'admin/template';
public function __construct()
{
parent::__construct();
$this->template = View::set_filename($this->template);
$this->template->css = 'adminstyles';
$this->template->js = 'html5';
}
}
However, I'm getting an error that css variable isn't defined within the "admin/template" file. At this point, the template file is identical to the template file in the views folder that I successfully used to create much of the front end so it's not there. Also, an important fact to note, when I use the template file in the views folder (for the front end) it loads the page correctly.
That leads me to believe it has something to do with the template file being located in a sub folder. That puzzles me though because I successfully load another view file from the same "views/admin" folder. That file doesn't contain any variables though, so maybe that's why it loads.
I've tried
$this->template = View::factory($this->template);
To load the new template file also, but it returns an error that the view must be called before rendering. I think that's due to auto render being on, but I want it on.
Any ideas would be a big help. Obviously, I could move the file out of the admin folder and rename it, but that's not really going to help me learn what's going on.
This line you removed:
$this->template = View::set_filename($this->template);
was breaking it because set_filename() is not a static method; that method should be called on an existing instance of a view because the method returns itself (useful for method chaining). However, you don't need to do that in the first place because you're extending the Template_Controller class which creates the template view for you in the constructor that you're calling with parent::_construct().
You would only need to use the set_filename() method if you have an existing template object but you want to change the view associated with it without having to recreate a whole new object.
Finally, the auto_render option triggers a post-controller hook that renders the view when the controller is finished; that would not have any effect on code inside any method of the controller class.
I've found the answer to my question. The line:
$this->template = View::set_filename($this->template);
needs to be removed. Maybe it loads the template again and cancels out the variables. I'm not sure exactly. However, I thought that line was necessary to change the template filename that was already defined in the Template Controller I was extending. I thought I read that correctly over at the Kohana forums. I suppose not. Hopefully this helps someone else.
the view subfolders should be named exactly as the controller that controls the views in the subfolder.
精彩评论