noodlehaus/dispatch

Issue loading views/layout using render()

GabrielGil opened this issue · 4 comments

Hello.

I'm not quite sure about whats the difference between "layout" and "view". In the render function the first parameter is ignored except when the third one is set to false.

Exactly backwards what is wrote on the documentation.

for example, if i execute render('view', array()); the layout loaded is "layout.html.php" in the views directory not the view.html.php as it's expected. Right?

To change the file included from the views folder, i have to set the third parameter, making the first one, even more useless.
render('view', array(), 'custom_view');

I'm not sure, if this is the expected behavior or not. If it is, the docs are not correctly explained, and i'm the first one who wanna know whats the difference between "view" and "layout".

Thanks!

Hi Gabriel. I'm sorry the docs isn't really clear in explaining this. I'll work on it later.

On you question regarding render(), there are some things about it:

  1. you need to set config('dispatch.views', 'path/to/views')
  2. you need to set config('dispatch.layout', 'layout-file')

Having those two configured, if you call render('viewname', array()), what it does is it loads 2 files:

  1. it first renders 'viewname' using the locals you passed through array()
  2. then the result from (1) gets plugged into the section of the layout file

By default, render() will use the layout file you specified via config('dispatch.layout'). But if for a page, you want to use a different layout, you can pass the layout name as the third parameter.

<?php
// setup views
config('dispatch.views', '../views');
config('dispatch.layout', 'layout');

// render page using default layout from dispatch.layout
render('index', array('name' => 'stranger'));

// render page using different layout file custom-layout.html.php
render('index', array('name' => 'stranger'), 'custom-layout');

// or render a page without a layout file
render('index', array('name' => 'stranger'), false);
?>

I hope this is a better explanation on how it works. If you have questions, keep posting them :) Thanks!

Yeah, in the documentation, there's no mention of how a layout file should be constructed. Will fix this in a while.

Yes, this is a better explanation. It works great. Thanks for your time.

The content() function doesn't appear anywhere in the documentation and it's a key point to understand this.

Thank you.

Thanks Gabriel. I've just updated the documentation as well -- 21fc79e.