• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DRY this view?

#1
Hi guys,

How do i shorten and make this view codes DRY ?

Code:
    public function show_sample()
    {
        $this->data['header']     = $this->load->view('public/templates/header', $this->data, TRUE);
        $this->data['menu']     = $this->load->view('public/templates/menu', $this->data, TRUE);
        $this->data['body']     = $this->load->view('public/templates/body', $this->data, TRUE);
        $this->data['footer']     = $this->load->view('public/templates/footer', $this->data, TRUE);
        $this->load->view('public/templates/master', $this->data);
    }

Thanks in advance.
Reply

#2
I really don't like the disease called 'Longlinitis'.
So this is what I did so far.
Not tested tho.

Here is the helper method.
Code:
/***
*     author: warren nazareno
*  email: [email protected]
*  purpose: will DRY the view codes.
***/
if (! function_exists('sview'))
{
    function sview($this->data)
    {
    
        $this->data['header']     = $this->load->view('/public/templates/header', $this->data, TRUE);
        $this->data['menu']     = $this->load->view('/public/templates/menu', $this->data, TRUE);
        $this->data['body']     = $this->load->view('/public/templates/body', $this->data, TRUE);
        $this->data['navleft']     = $this->load->view('/public/templates/navleft', $this->data, TRUE);
        $this->data['navright']    = $this->load->view('/public/templates/navright', $this->data, TRUE);                
        $this->data['footer']     = $this->load->view('/public/templates/footer', $this->data, TRUE);
        
        $this->load->view('/public/templates/master', $this->data);
    }
}

Here is the controller method.
Code:
    public function show_master() {
        sview($this->data);    
    }

I'm currently experimenting this.
If you guys have suggestion and have better codes than this please share.

Thanks in advance.
Reply

#3
Here is the update for these codes,
I move the method into Application/Core/MY_controller.php
Code:
    /***
     *     author: warren nazareno
     *  email: [email protected]
     *  purpose: will DRY the view codes.
     ***/
    function sview($data=array())
    {
        $this->data['header']         = $this->load->view('/public/templates/header', $this->data, TRUE);
        $this->data['menu']         = $this->load->view('/public/templates/menu', $this->data, TRUE);
        $this->data['body']         = $this->load->view('/public/templates/body', $this->data, TRUE);
        $this->data['navleft']         = $this->load->view('/public/templates/navleft', $this->data, TRUE);
        $this->data['navright']        = $this->load->view('/public/templates/navright', $this->data, TRUE);                
        $this->data['footer']         = $this->load->view('/public/templates/footer', $this->data, TRUE);
        
        $this->load->view('/public/templates/master', $this->data);
    }

Because it's throwing me error message when I put it under helper.
And now it's working fine.
Again if you guys have better suggestion please let me know.
Reply

#4
And the controller looks like this.
Code:
    public function show_master() {
        $this->sview($this->data);    
    }
Reply

#5
I might make the method sview() look like this below,
Code:
function sview($header, $body, $header, $data=array())
    {
       ...
    }

to make it more dynamic.
tweaking is still in progress...
suggestion is welcome...
Reply

#6
(09-13-2015, 07:56 AM)solidcodes Wrote: Here is the update for these codes,
I move the method into Application/Core/MY_controller.php


Code:
/***
* author: warren nazareno
*  email: [email protected]
*  purpose: will DRY the view codes.
***/
function sview($data=array())
{
$this->data['header'] = $this->load->view('/public/templates/header', $this->data, TRUE);
$this->data['menu'] = $this->load->view('/public/templates/menu', $this->data, TRUE);
$this->data['body'] = $this->load->view('/public/templates/body', $this->data, TRUE);
$this->data['navleft'] = $this->load->view('/public/templates/navleft', $this->data, TRUE);
$this->data['navright'] = $this->load->view('/public/templates/navright', $this->data, TRUE);
$this->data['footer'] = $this->load->view('/public/templates/footer', $this->data, TRUE);

$this->load->view('/public/templates/master', $this->data);
}

Because it's throwing me error message when I put it under helper.
And now it's working fine.
Again if you guys have better suggestion please let me know.

I think your problem is in sview() declaration. If function param is $data, so in the body you should use $data, not $this->data.
Reply

#7
(09-13-2015, 07:56 AM)solidcodes Wrote: Here is the update for these codes,
I move the method into Application/Core/MY_controller.php
Code:
    /***
     *     author: warren nazareno
     *  email: [email protected]
     *  purpose: will DRY the view codes.
     ***/
    function sview($data=array())
    {
        $this->data['header']         = $this->load->view('/public/templates/header', $this->data, TRUE);
        $this->data['menu']         = $this->load->view('/public/templates/menu', $this->data, TRUE);
        $this->data['body']         = $this->load->view('/public/templates/body', $this->data, TRUE);
        $this->data['navleft']         = $this->load->view('/public/templates/navleft', $this->data, TRUE);
        $this->data['navright']        = $this->load->view('/public/templates/navright', $this->data, TRUE);                
        $this->data['footer']         = $this->load->view('/public/templates/footer', $this->data, TRUE);
        
        $this->load->view('/public/templates/master', $this->data);
    }

Because it's throwing me error message when I put it under helper.
And now it's working fine.
Again if you guys have better suggestion please let me know.


and you should create $data for header|footer|etc in all places where you need this header|footer|etc.

and what about if we hav header|footer|menu|etc as classes with on controllers,dates,viewers,etc andjust call it in needed places.

My working approach.

Code:
# cat ./MY_Loader.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

        protected $CI;

        public function __construct(){
            $this->CI =& get_instance();
        }

        public function commons($file_name,$method,$data = FALSE) {

                    $file_path = APPPATH . 'controllers/common/' . ucfirst($file_name) . '.php';
                    $object_name = $file_name;
                    $class_name = ucfirst($file_name);

                    if(file_exists($file_path)){
                            require_once $file_path;

                            $this->CI->$object_name = new $class_name();
                            $this->CI->$object_name->$method($data);

                    } else {

                            show_error("Unable to load the requested controller class: " . $class_name);
                    }
        }
}

And in any page just

$this->load->commons('header','index',$data_header);
$this->load->commons('left','index');
$this->load->view('client_list',$data);
$this->load->commons('right','index');
$this->load->commons('footer','index');
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.