Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
3 ways of rendering a view in FuelPHP
  • 1. In our controller class, we can do
    $this->response->body = View::factory('path/to/view'); 2. In our controller class, we can do
    $data = array('key1' => 'value1', 'key2' => 'value2');
    $this->render('path/to/view', $data); 3. In our controller class, we can do
    $data = array('key1' => 'value1', 'key2' => 'value2');
    $view = new View('path/to/view', $data);
    print $view->render();
  • Maybe in your controller. But in a standard controller, no method render() exists (your option 2). And View::factory() does (currently) the same as new View(); I would suggest to use the factory/forge methods where available, as some do more than only instantiate the object. Also, in the future more functionality could be added to those methods, or existing functionality could be altered, which makes option 3 not a very smart idea.
  • I have a quick follow-up on this. Controllers should indeed return their output (can be a plain string, a View object or a Response object). Controller_Template still behaves differently and doesn't return their output. I know they can have multiples variables so it's technically tricky, but being able to return their output would be useful too. It could be assigned automatically to the $body var. Even be configurable with a $template_var assigned in the same time as $template. What do you think ?
  • In fuel/core/classes/controller.php, there is a render method: /**
    * Render a view and add it to the body
    *
    * @param string path to the view
    * @param array variables for the view
    * @param bool|null whether to use output encoding
    */
    public function render($view, $data = array(), $auto_encode = null)
    {
    $this->response->body .= \View::factory($view, $data, $auto_encode);
    } So if our controller extends the default core controller, we can use the render method.
  • Harro Verton wrote on Tuesday 20th of September 2011:
    Maybe in your controller. But in a standard controller, no method render() exists (your option 2).

    https://github.com/fuel/core/blob/1.0/master/classes/controller.php#L86
  • The current development docs don't mention the render() method anymore, but indeed, the controller still contains the method. I think that needs to be changed, as $this->response->body is deprecated, in v1.1, controllers need to return their output.
  • Indeed, I think returning output from the controller will conform more on HMVC design pattern. And we can move the restful call from the parent request to the sub controllers easier.
  • In 1.1 RC, we can render views like the following:
    class Controller_Default extends Controller{
      public function action_index(){
        $data = array('number' => 12);
        //the variable &number; will be available in view file default/index.php
        return View::forge("default/index",$data);  
      }
    
    }
    
  • Controller_Template returns in the after() method, and it does return a Response object.

Howdy, Stranger!

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

In this Discussion