Welcome Guest, Not a member yet? Register   Sign In
Setting title, description and keyword tags in the view rather than in the controller?
#1

[eluser]Unknown[/eluser]
Hello Igniters of Code,

Is it possible with CI to set the title in the view rather than the controller? Currently, I'm setting the title in each controller method (works as expected), but it sure seems like I should be doing that kind of thing in the views instead.

If I can figure that out, then I can also set the description and keyword tags from the view as well. Google will love me!

Maybe, oh maybe even use this magic to figure out a way to include a javascript that is only used in that particular view as well...?

I would very much appreciate cleaning up my controllers by figuring this out (with your help). I've searched around and haven't found any reference to this sort of tomfoolery. Has anyone done something like this with CI?

Many thanks,
#2

[eluser]theprodigy[/eluser]
I normally use MY_Controller to get data that will be used on every page (page specific title, meta tags, javascript, css, etc). In the MY_Controller, I just set variables for what they will be (or arrays in the case of javascript and css), and pass these variables to the view to use (depending on site, I sometimes just set css and js in controller methods rather than MY_Controller). MY_Controller extends Controller, and all my controllers extend MY_Controller

Controller:
Code:
$this->get_page_info(1); // number passed in is page id in database

MY_Controller:
Code:
public function get_page_info($page_id)
{
    $this->load->model('page_model','page');
    $page_data = $this->page->get_data($page_id);

    $this->data['title'] = $page_data->title;
    $this->data['meta_desc'] = $page_data->meta_desc;
    $this->data['meta_keywords'] = $page_data->meta_keywords;
        
    $this->load->vars($this->data);
    $this->load->view('layout');
}

Page_model:
Code:
public function get_data($page_id)
{
    $this->db->select('title, meta_desc, meta_keywords[, any other data you want]');
    $this->db->from('pages');
    $this->db->where('id', $page_id);
    $page = $this->db->get();
        
    if($page->num_rows() > 0)
    {
        return $page->row(); //you are getting data by primary key, assume only one row returned
    }
    else
    {
        return false;
    }
}

Layout.php
Code:
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $meta_desc; ?>">
<meta name="keywords" content="<?php echo $meta_keywords; ?>">
</head>
<body>
/*
content goes here. Data array was loaded rather then passed in, so you can call other views from here and variables will be available inside them as well. Which is the reason I use a layout.php for my basic setup, then call in page specific views from there. Makes it easier that way.
*/
</body>
</html>
#3

[eluser]jedd[/eluser]
Hi backwardm and welcome to the CI forums.

[quote author="backwardm" date="1260443554"]
Is it possible with CI to set the title in the view rather than the controller?
[/quote]

Can't you just:
Code:
// view file
...
<title>
This is my view-set title
</title>

Though I'm not sure why you want to set a title in a view - typically the title is derivative of where you are in the application - something a view doesn't necessarily have a good understanding of.

Quote:Maybe, oh maybe even use this magic to figure out a way to include a javascript that is only used in that particular view as well...?

What problems have you had when you insert JS into the view file?




Theme © iAndrew 2016 - Forum software by © MyBB