Welcome Guest, Not a member yet? Register   Sign In
Poll: Should something like this view_layout method be included?
You do not have permission to vote in this poll.
Yes
30.00%
3 30.00%
No
60.00%
6 60.00%
Maybe - see comments
10.00%
1 10.00%
Total 10 vote(s) 100%
* You voted for this item. [Show Results]

View Layouts - Input Desired
#1

Currently, in CI4 we can render out a view with the view() method:

Code:
echo view('welcome_message', $data);

I was toying around with a very simple idea tonight, and wanted to know if people thought it was worth including or not: view layouts. This provides a very simple way to wrap one or more views within a common layout file, making a very bare bones theme-able system that works like this:

1. A common file exists at Views/Theme/Index.php. This would include common headers, footers, maybe a sidebar, etc.
2. We call it something like this:

Code:
echo view_layout('Theme/index', $layout_data, function() use ($data) {
   echo view('welcome_message', $data);
});

3. The output of the closure is captured, and placed into the $layout_data variable with the name $layout_content, so the layout would need to echo that out at where it wants it to show up.

Code:
<?= $layout_content ?>

Is this useful? Too simplistic to be worth anything, too complex to use on every method? Curious what everyone thinks.

For those that want to play around with the concept, you can add this small method to the Common.php file:

Code:
function view_layout(string $layout, array $data = [], Closure $closure)
{
// Catch any views rendered in the closure
ob_start();
$closure();
$buffer = ob_get_clean();

// Add them to the data
$data['layout_content'] = $buffer;

return view($layout, $data);
}
Reply


Messages In This Thread
View Layouts - Input Desired - by kilishan - 07-10-2016, 09:05 PM
RE: View Layouts - Input Desired - by PaulD - 07-11-2016, 06:09 AM
RE: View Layouts - Input Desired - by sintakonte - 07-11-2016, 07:13 AM
RE: View Layouts - Input Desired - by prezire - 07-11-2016, 05:42 PM
RE: View Layouts - Input Desired - by PaulD - 07-11-2016, 09:39 PM
RE: View Layouts - Input Desired - by albertleao - 07-11-2016, 09:52 PM
RE: View Layouts - Input Desired - by kilishan - 07-11-2016, 09:59 PM
RE: View Layouts - Input Desired - by sv3tli0 - 11-05-2016, 01:07 AM
RE: View Layouts - Input Desired - by cartalot - 11-17-2016, 01:29 PM
RE: View Layouts - Input Desired - by PaulD - 11-17-2016, 03:50 PM



Theme © iAndrew 2016 - Forum software by © MyBB