Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Global template replace by other
  • Hello. I have problems to replace global template.
    class Controller_Admin extends Controller_Base {
    
     public $template = 'admin/template';
    
     public function before()
     {
      parent::before();
    
      if ( ! Auth::member(100) and Request::active()->action != 'login')
      {
       Response::redirect('admin/login');
      }
     }
    

    Controller admin has defined global $template. I need create login action with replacing global template. How can I do it.
  • You can set $this->template in you before() method.
  • Hi, so I think I'm having the same issue here - the before() method doesn't appear to want to let me set a different template to the regular one. The background is that I want to set a different template if a mobile device is detected. Here's some code:
    class Controller_Site extends Controller_Template
    {
        public $template = 'template_main';
    
        public function before()
        {
            parent::before();
            
            if ($this->IsMobileUserAgent())
            {
                $this->template = 'template_mobile';
            }
        }
    
        public function action_index()
        {     
            $this->template->title = 'The title';
            $view = View::forge('site/index');
            $this->template->content = $view;
        }
        
        // The code to detect a user agent is large - this is just a stub as an example
        private function IsMobileUserAgent()
        {
            // If it's a mobile browser, return true
            // else return false - code from [url=http://detectmobilebrowsers.com/]http://detectmobilebrowsers.com/[/url]
            return true;
        }
    }
    

    The page just displays "template_mobile" and seems to ignore templating altogether. I guess I'm doing something obviously wrong, but I'm not sure what. If I change it to not have the before method, and to just use template_main, it works okay. It's only when I try and change the template in the controller that I have a problem. Any suggestions gratefully received.
  • Problem here is that $this->template is processed in the parent::before(), so if your before() need logic that alters the template, do that BEFORE you call parent::before().
  • Perfect, sorted. Thank you very much! Easy when you know how. :-)

Howdy, Stranger!

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

In this Discussion