0

Referring to the top answer on this post:

Header and footer in CodeIgniter

How could you update this class to support multiple views if required?

e.g. sometimes loading two or more views between the header and footer templates...

Thanks in advance :)

Community
  • 1
  • 1
Random
  • 97
  • 1
  • 11

2 Answers2

1

If you want each view to have it's own vars:

public function template($template_names = array(), $vars = array(), $return = FALSE)
{
    $content  = $this->view('templates/header', $vars, $return);
    foreach ($template_names as $template_name -> $template_vars)
    {
        $content .= $this->view($template_name, $template_vars, $return);
    }
    $content .= $this->view('templates/footer', $vars, $return);

    if ($return)
    {
        return $content;
    }
}

.

$this->load->template(array(
    'body' => $vars_for_body,
    'body2' => $vars_for_body2,
    'body3' => $vars_for_body3
), $headerfooter_vars);
Samutz
  • 2,310
  • 4
  • 24
  • 29
  • 1
    Thank you for your answer - i almost had something like this :) P.S thank you to Hashem Qolami for providing a solution too. I went for Samutz in the end. – Random Feb 06 '14 at 21:55
  • @Random You're welcome, I tried to implement a little more complex algorithm. which gives you a better functionality. – Hashem Qolami Feb 06 '14 at 22:00
1

Well, here is my attempt to improve the functionality of the template function:

class MY_Loader extends CI_Loader {
    public function template($template_name = array(), $vars = array(), $return = FALSE)
    {
        $content  = $this->view('templates/header', $vars, $return);

        if (is_array($template_name)) {
            foreach ($template_name as $view => $viewVar) {
                // Whether the view has different variables
                if (is_array($var) && ! is_numeric($view)) {
                    // Load the view with its own variables
                    $content .= $this->view($temp, $viewVar, $return);
                } else {
                    // Load the view whith the general variables $vars
                    // viewVar would be the view name in this case
                    $content .= $this->view($viewVar, $vars, $return);
                }  
            }
        } else {
            $content .= $this->view($template_name, $vars, $return);
        }

        $content .= $this->view('templates/footer', $vars, $return);

        if ($return)
        {
            return $content;
        }
    }
}

Using this, you could load the views in the format below:

$this->load->template(array(
    'first/view' => array('name' => 'value'),
    'second/view',
    'third/view'
), $generalData);

Each view can have its own variables.

In this case, The first view is loaded by passing the array('name' => 'value') as its variable. And the second/third views are loaded with $generalData as the variable.

If you need go get access to $generalData in the first view, you could use + operator to merge the variables as: array('name' => 'value') + $generalData or vice versa.

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164