Welcome Guest, Not a member yet? Register   Sign In
TUTORIAL : templates using nothing but CI code
#1

[eluser]xwero[/eluser]
I see a lot of people are struggling to remove common page parts from the controller methods. Rather than writing use this or that library i decided to share a way to do this without third party code.

What do we need:
- one developer
- hooks enabled
- know that the load->vars method exists

The two hooks we are going to use are the post_controller_constructor and post_controller. The post_controller_constructor loads common variables and if you don't use a template you can load your header. The post_controller loads the template or the footer.

Let's see some code in case you want to load header and footer.
application/hooks/page_parts.php
Code:
function header()
{
   $CI =& get_instance(); // possible because the hook is loaded after the superobject is formed
   $CI->load->view('pageparts/header');
}

function footer()
{
   $CI =& get_instance();
   $CI->load->view('pageparts/footer');
}
application/config/hooks.php
Code:
$hook['post_controller_constructor'] = array(
                                'function' => 'header',
                                'filename' => 'page_parts.php',
                                'filepath' => 'hooks',
                                );

$hook['post_controller'] = array(
                                'function' => 'footer',
                                'filename' => 'page_parts.php',
                                'filepath' => 'hooks',
                                );
And that is it! Because the header view is loaded after the constructor you can pass variables to it in the constructor keeping the hook function as lean as possible.

For a template it's almost the same but then there is no need to have a header function.
application/views/pageparts/template.php
Code:
<html>
<head>
<title><?php echo $page_title ?></title>
</head>
<body>
<div id="header">&lt;?php echo $header ?&gt;</div>
<div id="content">&lt;?php echo $content ?&gt;</div>
<div id="footer">&lt;?php echo $footer ?&gt;</div>
&lt;/body&gt;
&lt;html&gt;
application/hooks/page_parts.php
Code:
function post_constructor()
{
   $CI =& get_instance();
   $CI->load->vars(array(  // added because errors don't look good on a site
                     'page_title'=>'Hello World',
                     'header'=>'header',
                     'content'=>'content',
                     'footer'=>'footer',
                   ));
}

function post_controller()
{
   $CI =& get_instance();
   $CI->load->view('pageparts/template');
}
application/config/hooks.php
Code:
$hook['post_controller_constructor'] = array(
                                'function' => 'post_constructor',
                                'filename' => 'page_parts.php',
                                'filepath' => 'hooks',
                                );

$hook['post_controller'] = array(
                                'function' => 'post_controller',
                                'filename' => 'page_parts.php',
                                'filepath' => 'hooks',
                                );
application/controllers/random_controller.php
Code:
class Random_controller extends Controller
{
    function index()
    {
         $vars['page_title'] = 'Random title';
         // You can set the default header and footer in the post_constructor function if you want
         $vars['header'] = $this->load->view('some_header');
         $vars['content'] = $this->load->view('some_content');
         $vars['footer'] = $this->load->view('some_footer');
         $this->load->vars($vars);
    }
}
You can add the page part variables as the second parameter of the load->view method but you can add them to the vars array too. The only thing you have to know is that in both cases the variables that are know before will be overwritten by the newer entries.

And this concludes the tutorial. Enjoy the end year festivities!
#2

[eluser]manilodisan[/eluser]
We use something else but similar to avoid repetitive code such as headers, footers, menus etc. I like smart, good practices. Thanks for sharing @xwero.




Theme © iAndrew 2016 - Forum software by © MyBB