Welcome Guest, Not a member yet? Register   Sign In
Proper way for controlling meta tags.
#1

[eluser]mTuran[/eluser]
In my web site have some meta tags like description or keywords sure. I want to change them from controllers dynamically one way to do that is:

Code:
<title><?=$page_title;?></title>
    <meta name="language" content="en" />  
    <meta name="description" content="<?=$page_description;?>" />

Code:
$data['page_title'] = 'Bla bla bla';
                $data['page_description'] = 'Bla bla bla';
        $this->load->vars($data);
        $this->load->view('layout/default');

This way works but i don't think it is proper way because if i want to write nothing to page_description and not write $data['page_description'], there will be occur notice errors. I have a idea. I think if i code meta library and add to auto load, i can easly control meta tags like $this->meta->set_desc('bla bla bla');

Is this proper way ? Thank you
#2

[eluser]Maglok[/eluser]
I think the most 'proper' way CI has of handling this is the html helper meta() function: http://ellislab.com/codeigniter/user-gui...elper.html
#3

[eluser]mTuran[/eluser]
i think you didn't understand my question. meta helper helps you for generate meta tags easily. my question is which way is proper for include my view.
#4

[eluser]Maglok[/eluser]
I'd say put this in your controller:

Code:
$meta = array(
        array('name' => 'robots', 'content' => 'no-cache'),
        array('name' => 'description', 'content' => 'My Great Site'),
        array('name' => 'keywords', 'content' => 'love, passion, intrigue, deception'),
        array('name' => 'robots', 'content' => 'no-cache'),
        array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv')
    );

Listing whatever you want. Then use the html helper to generate them in your viewer like so:

Code:
<?php echo meta($meta); ?>
#5

[eluser]mTuran[/eluser]
if i don't declare $meta variable in my controller, there will be occur php notice error. Am i wrong ?

I will use this helper in my library. Point of i want to write library is if i don't declare any meta tag, library will handle it and there will be not php notice error.
#6

[eluser]Maglok[/eluser]
Use a IF construction? Something like:

Code:
<?php if(isset($meta)) {
  echo meta($meta));
} else {
<--! Do nothing !--&gt;
?&gt;
}
#7

[eluser]mTuran[/eluser]
is using if conditions in views a good chioice ? I think it's a bit of hacky. Thanks
#8

[eluser]Phil Sturgeon[/eluser]
Perhaps the shorter syntax of:

Code:
&lt;?php echo meta(@$meta); ?&gt;

or

Code:
&lt;?php if(isset($meta)) meta($meta); ?&gt;

or

Code:
&lt;?=isset($meta) ? meta($meta) : ''; ?&gt;

How did I miss the meta helper? I will have to use this in my code instead of my rather limited meta string calculations based on name in my layout library.
#9

[eluser]Maglok[/eluser]
It's 'hidden' in the html helper. I guess it is the best place for it, but not very obvious. Easy to miss, very usefull though, VERY usefull.
#10

[eluser]mTuran[/eluser]
i will use meta(@$meta); thanks.

I have one more little question. My default layout like that

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
    &lt;title&gt;&lt;?=$page_title;?&gt;&lt;/title&gt;
    &lt;meta name="language" content="tr" /&gt;  
.....

If one day i decide to add one more variable to my default layout view then i have to edit all of my controllers because of notice error. I don't understand this side of MVC. Thanks




Theme © iAndrew 2016 - Forum software by © MyBB