English 中文(简体)
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 using 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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签