Welcome Guest, Not a member yet? Register   Sign In
Default view for all pages
#1

[eluser]garrettheel[/eluser]
Just wondering what the best way to go about using a default view for all pages would be. Coming from a Cake PHP background, it would be something similar to the default view for cakephp where you have your header and footer in a view, and then you have a variable inbetween which inserts all of middle content.
#2

[eluser]xwero[/eluser]
a quick an dirty way to do it is to set up a post-controller hook where you load the site/section template and use the load->vars method for adding the partial views and the content for those views. You don't have the syntactic sugar but it gets the job done.
#3

[eluser]garrettheel[/eluser]
There's no clean and simple way to do it then? Surely this is done on most applications so there should be some sort of common way that everyone does it.
#4

[eluser]xwero[/eluser]
There are libraries you can check out that provide a clean way but i don't think it can be simpler than the solution i proposed as it uses the already existing functionality of the framework.
Code:
// template.php
function get_template() // basic function
{
   $CI =& get_instance();
   $CI->load->view('layout');
}
// config/hooks.php
$hook['post_controller'] = array(
                                'class'    => '',
                                'function' => 'get_template',
                                'filename' => 'template.php',
                                'filepath' => 'hooks',
                                );
// views/layout.php
<html>
<head>
<title></title>
<head>
<body>
<?php echo $content ?>
</body>
</html>
// some_controller.php
function some_function()
{
   $array = array('some_view_var'=>'value',
                  'content'=>$this->load->view('some_view','',TRUE));
   $this->load->vars($array);
}
The some_view_var value is magically attached to the var in some_view and the some_view is added to the layout. I think it's a very flexible way of using a base template layout.




Theme © iAndrew 2016 - Forum software by © MyBB