Welcome Guest, Not a member yet? Register   Sign In
Can I send dynamic info the the title tag with $this->load->vars() and extending the controller class?
#1

[eluser]Flying Fish[/eluser]
I'm extending the controller class with MY_Controller and using this code:
Code:
$this->load->vars('head', $this->load->view('_inc/head', '', TRUE));
To create commmon template pieces like a header footer sidebar etc.

Can I pass data from a controller method to one of these vars so I can have a unique <title> on each page?

like in my controller, I would have
Code:
$data['title'] = "fish food";
$this->load->view('page_view', $data);

and in the view I have
Code:
<head>
<?=$head?>
</head
#2

[eluser]TheFuzzy0ne[/eluser]
I use this function (thanks to zwero for showing me):

./system/application/libraries/MY_Loader.php
Code:
<?php

class MY_Loader extends CI_Loader {
        
    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;
                }
            
            }
        }
    }
}

So loading some data from an imaginary model, and loading the a view using that data, I can do something like this:

Code:
$this->load->vars(array(
        'head_data' => $this->my_model->get_head_data(),
        'view:head' => 'head_view'    # This is where the magic happens, the view is
        # loaded into a variable named "head". Just ensure that you remember to load the
        # variables needed by the view before you load the view variable itself.
    ));

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

Hope this helps.
#3

[eluser]Flying Fish[/eluser]
thanks!

Looks a little bit complicated, but I may try to implement it.

I guess I could also take the cowards way out and do something like this.

MY_Controller
Code:
$this->load->vars('doctype', $this->load->view('_inc/head', '', TRUE));
// doctype and opening head tag

$this->load->vars('head', $this->load->view('_inc/head', '', TRUE));
// all my css and js and meta stuff
// and closing head tag

then in the view
Code:
<?=$doctype?>

<title>Page Title Here</title>

<?=$head?>
#4

[eluser]TheFuzzy0ne[/eluser]
Yes, that will work just fine (so long as you've loaded any variable dependencies first, of course).

Sorry, it never occurred to me that $this->load->vars() accepted a pair of parameters. I'm too used to calling it all with a single array.




Theme © iAndrew 2016 - Forum software by © MyBB