Welcome Guest, Not a member yet? Register   Sign In
Loading views within views
#1

[eluser]TheFuzzy0ne[/eluser]
Hi. I'm wondering if anyone can offer me a more elegant solution to the one I have.

I have a main template which is loaded, and within that template, I echo the contents, and sidebar. My controller code looks something like this:

Code:
$content_data['title'] = 'Recommended Resources';
$content_data['body_id'] = 'recommendedResources';
$content_data['resources'] = $this->my_model->getResources();
        
$sidebar_data['categories'] = $this->my_model->getCategories();
        
$data['content'] = $this->load->view('recommended_resources/index', $content_data, TRUE);
$data['sidebar'] = $this->load->view('recommended_resources/cats_right_nav', $sidebar_data, TRUE);

$this->load->view('main_template', $data);

That looks quite messy to me, and I will probably need to add more variables at some point also. Does that look right, or is it overkill?

Many thanks in advance.
#2

[eluser]xwero[/eluser]
I'm a big fan of the vars method but then you have to be careful later variables don't overwrite the older. Your code can look like this
Code:
$this->load->vars(array(
                    // content
                    'title'=>'Recommended Resources',
                    'body_id'=>'recommendedResources',
                    'resources'=>$this->my_model->getResources(),
                    // sidebar
                    'categories'=>$this->my_model->getCategories(),
                    // main_template
                    'content'=>$this->load->view('recommended_resources/index', '', TRUE),
                    'sidebar'=>$this->load->view('recommended_resources/cats_right_nav', '', TRUE),
                  ));

$this->load->view('main_template');
And all your variables will be automagically added to the correct views.
#3

[eluser]pistolPete[/eluser]
You could have a look at the numerous "template/layout libraries":

- http://www.williamsconcepts.com/ci/codei.../template/

- http://codeigniter.com/wiki/View_Object_PHP5

- http://codeigniter.com/wiki/View_Object/

And so on...
#4

[eluser]TheFuzzy0ne[/eluser]
Thanks for the replies, guys. I'd like to avoid using a library if possible, as it's probably overkill for what I need. I had no idea $this->load->vars() was so powerful... I think I'll use that.

Thanks again, guys!
#5

[eluser]TheFuzzy0ne[/eluser]
Ack! $this->load->vars() is not working. The views are being loaded before the variables have been unpacked.
#6

[eluser]TheFuzzy0ne[/eluser]
I think the calls to $this->load->view() should come after the call to $this->load->vars().
#7

[eluser]pistolPete[/eluser]
I don't want to be rude, but you do know there is an edit button? Smile
#8

[eluser]TheFuzzy0ne[/eluser]
I just needed to call $this->load->vars() twice:

Code:
$this->load->vars(array(
                'title' => 'Recommended Resources',
                'body_id' => 'recommendedResources',
                'resources' => $this->recres_model->getResources(),
                'categories' => $this->recres_model->getAllCategories()
            ));
            
        $this->load->vars(array(
                'content' => $this->load->view('recommended_resources/index', '', TRUE),
                'sidebar' => $this->load->view('recommended_resources/cats_right_nav', '', TRUE)
            ));
            
        $this->load->view('main_template');
#9

[eluser]TheFuzzy0ne[/eluser]
[quote author="pistolPete" date="1235424511"]I don't want to be rude, but you do know there is an edit button? Smile[/quote]

Yes I do, but you two guys seem to be on the ball today, so I didn't want you both to miss any edits I made.

Your comment is duly noted.
#10

[eluser]xwero[/eluser]
Ah yes i know why this happens. The view method is executed in the controller before the vars method has a chance to add the variables.

I changed the vars method a bit
Code:
function vars($vars = array(), $val = '')
    {
        if ($val != '' AND is_string($vars))
        {
            $vars = array($vars => $val);
        }

        $vars = $this->_ci_object_to_array($vars);

        if (is_array($vars) AND count($vars) > 0)
        {
            foreach ($vars as $key => $val)
            {
                if(strncmp('view:',$key,5) == 0)
                {
                   $this->_ci_cached_vars[substr($key,5)] = $this->view($val,'',TRUE);
                }
                else
                {
                    $this->_ci_cached_vars[$key] = $val;
                }

            }
        }
    }
And now you can do
Code:
$this->load->vars(array(
                    // content
                    'title'=>'Recommended Resources',
                    'body_id'=>'recommendedResources',
                    'resources'=>$this->my_model->getResources(),
                    // sidebar
                    'categories'=>$this->my_model->getCategories(),
                    // main_template
                    'view:content'=>'recommended_resources/index',
                    'view:sidebar'=>'recommended_resources/cats_right_nav',
                  ));

$this->load->view('main_template');




Theme © iAndrew 2016 - Forum software by © MyBB