Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
New to MVC and FuelPHP question about views and partial views
  • I am using the Template controller and would like to have a single view contain several partial views. The way this is done as I understand it is to create a template with variable that contain the partials and echo them out from the master template. $this->template->header = View::forge('header', $data);
    $this->template->content = View::forge('posts', $data);
    $this->template->footer= View::forge('footer', $data); My question relates to if it is acceptable to load a view from within a view. For example a view named posts.php may be include within the view file a a reference to a view named comments.php. I do not need to include comments as part of my template as it is not needed on every page, but several pages have comments and I do not want to keep recreating the logic every time I need it. It would be much easier if I could just call the view from within the main view where it is needed. Is it ok? ... for some reason it just feels like it is wrong. I am new to frameworks and FuelPHP so I want to learn the right way out of the gate instead of the wrong way from the get go. Is there a way to load both views into the template from the controller. $this->template->content = View::forge('Posts',$data);
    $this->template->content .= View::forge('Comments', $data); I have read over the documentation and can not for the life of me find an answer. Any help is greatly appreciated!
  • FuelPHP utilizes a technique called late rendering. Which means that views will exist as objects until it is needed to get their string representation, usually when you echo it. This saves processing because rendering only happens when needed, not every time you forge a view. So technically you are already rendering views in views. As to your question, I would pass the comments view object as part of the data to the Posts view. In that view, check if the variable is present, and if so, echo it. It is a better solution then string concatenation, as that would force the view to be cast to string, which means it's going to be rendered there, and not when you output it.
  • Thank you. As a newbie I just wanted to make sure that i was doing things the right way.

Howdy, Stranger!

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

In this Discussion