Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Exclude a method from $this->template-> | use $this->response->body instead
  • <edit>
    Skip this discussion, head over to the dev-docs about the Template Controller
    </edit>
    Hi, I was wondering if I could exclude a method from $this->template-> and use $this->response->body instead. Let me explain: Like many here, I'm writing my own admin panel.
    The layout of the admin panel differs greatly from the layout of my website, so I use a different template.
    I use a basecontroller for my admin panel, which extends the controller_template
    class Controller_Admin extends Controller_Template {
        
        public $template = 'template_admin';
        
        public function action_index() { } // show dashboard
    
        [b]public function action_login() { }[/b]
    
        public function action_logout() { }
    
        public function action_news() { } // show all news items
    
        public function ...
    }
    

    This admin panel will have links to all the admin options: news (add, edit, remove), user (add, edit, remove), ...
    Ofcourse, when an admin still has to login, you don't want to show these links. So my question is: can I exclude (in this example) the login method from the template system and only show the login fields?
    In all other admin methods, I would like to use the admin template... Or, if this is impossible, what is the best practice for this? Jeroen
  • Looking for the solution too. The "solution" I use now is to create a separate controller for the page which you need to exlude from the normal template and change the template for that controller, but there must be a more simple way.
  • Normally, you would use a public base controller for unauthenticated output, and a different one for authenticated output. A login page is per definition unauthenticated, so it's controller would use the public base controller.
  • Harro Verton wrote on Sunday 7th of August 2011:
    Normally, you would use a public base controller for unauthenticated output, and a different one for authenticated output. A login page is per definition unauthenticated, so it's controller would use the public base controller.

    But there are other situations in which you need to exlude a method from the default template, like a form in a lightbox or a landing page in a website for example.
  • If you had looked at the code of Controller_Template, you would have seen how it deals with the defined template. And it also makes it very clear how to disable the auto rendering behavior: simply set $this->auto_render to false, in your before(), before calling parent::before().
  • The way I have it setup is I have a core controller, and then I have a site controller and admin controller, both of which extend this core controller. The core has methods that are used across both the site/admin controllers, and then the site/admin controllers each have their own specific methods/before operations and so on and so forth.
  • Harro Verton wrote on Sunday 7th of August 2011:
    If you had looked at the code of Controller_Template, you would have seen how it deals with the defined template. And it also makes it very clear how to disable the auto rendering behavior: simply set $this->auto_render to false, in your before(), before calling parent::before().

    Coming from CI, I am used to check out the docs, not the source code.
    From now on, I will be diving in the code before asking stupid questions ;) Anyway: solution to my problem:
    class Controller_Admin extends Controller_Template {
    
        /**
         * @var string page template
         */
        public $template = 'template_admin';
    
    //--------------------------------------------------------------------------------------------------------
    
        /**
         * Run this code before every other method in this class
         * 
         * @param array $data   // make Controller_Admin compatible with Controller_Template
         */
        public function before($data = null) {
    
            // Make sure the template system works
            parent::before();
    
            // Not autorendering templates makes it possible to exclude some methods from the template
            [b]$this->auto_render = false;[/b] // This line of code must be put [b]AFTER[/b] parent::before();
    
            // Load package auth
            Fuel::add_package('auth');
    
            ...
        }
    
    //--------------------------------------------------------------------------------------------------------
    
        public function action_index() {
            //load the dashboard
    
            $data = array();
            $this->template->content = View::factory('admin/index', $data);
            [b]$this->response->body = $this->template;[/b]  // Because we set auto_render to false, you need this line to output your template content
        }
    
    //--------------------------------------------------------------------------------------------------------
    
        public function action_login() {
             [b]$this->response->body = View::factory('admin/login');[/b] // This method is excluded from the template.
        }
    
    }
    
  • We're working very hard on getting the docs complete, but that's a long process, given the fact that we only have one person available that works on that. Therefore we invite the community to fork the docs repository on github, and make the modifications themselfs when they encounter an omission such as this.
  • Harro Verton wrote on Monday 8th of August 2011:
    We're working very hard on getting the docs complete, but that's a long process, given the fact that we only have one person available that works on that. Therefore we invite the community to fork the docs repository on github, and make the modifications themselfs when they encounter an omission such as this.

    I am new to git, so please help me a little.
    [I use bitbucket because of the free private repo's and never worked with just me on a project). I forked the fuel docs and updated the template doc.
    You can see this here: https://github.com/jeroenbrussich/docs Now:
    1. How do I get these changes/additions approved / back to you?
    2. How do I get to keep my fork up to date?
    I mean, what if the fuel docs change on git? Do I get a chance to update my fork or do I delete my fork and create a new one? --
    Jeroen
  • Github as an excellent help page on that: http://help.github.com/fork-a-repo/ Make sure you switch to the develop branch (git checkout develop) before doing anything. If you want to push updates back, make sure your changes are atomic, because you send us pull request of a commit. See http://help.github.com/send-pull-requests/ for more information on pull requests.
  • By making the changes atomic you mean... ? ---
    edit:
    The internet is sparse with information about 'atomic changes'.
    I think it means: don't commit multiple changes but keep changes together?? Like:
    - one commit for template
    - one commit for spell changes
    - one commit for ...
  • I mean don't commit to much in one go, with changes that don't belong together, as we have to merge the entire commit when you send us a pull request.
  • Harro Verton wrote on Monday 8th of August 2011:
    I mean don't commit to much in one go, with changes that don't belong together, as we have to merge the entire commit when you send us a pull request.

    I had some difficulties offering the right files to the pull request, but FreakyNet is studying the changes, so I guess I did ok. Sorry for the many questions, but now I understand what needs to be done if I want to cooperate... --
    Jeroen

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion