Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is render() optional?
  • I've been playing around with Fuel for the first time. If you look at the code below, which is just the welcome controller modified, the nested view on line 9 seems to work fine without the call to render(). So is it optional, or am I just doing things wrong? Also, I saw in another thread that $this->response->body is going to be deprecated. So, what should I be using instead?
    public function action_index()
    {
     $view = View::factory('welcome/index');
     
     $view->set(
      array(
       'title' => 'My Fuel',
       'name' => '<span>Jesus</span>',
       'partial' => View::factory('welcome/partial')->render() // Render seems optional ???
      ),
      NULL,
      FALSE
     );
    
     $this->response->body = $view;
    }
    
  • That depends on when you want the rendering to take place. If you use render(), the view is rendered immediately. If you don't, the __toString() magic method of the View class will render the view as soon as Fuel wants to output it. Delaying rendering is sometimes handy, for example when you use redirects() (if you do, no CPU is wasted rendering a view that isn't send to the browser), or if the view uses information that isn't present when you create the view object. As example of the last, I have a view in my applications that display messages at the top of my pages content block. So I have to create that and assign it to the page template before I call the method that generates the content body. When rendering directly, there is no way this method could add extra messages other then via javascript. No my methods call a static Messages class to add messages, the message view calls that class to get the messages when rendering the view. Due to 'lazy' rendering, this now happens AFTER the body content controller has run.

Howdy, Stranger!

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

In this Discussion