Welcome Guest, Not a member yet? Register   Sign In
Title, Description, and Keyword best practice
#1

[eluser]xerosis[/eluser]
I am creating a site which has many pages, each page has its own:
- title
- description
- keyword

Now I have been reading that is is not good practice to call a model from a view.

I have a file called header.php where title, description, and keywords are saved.

What I am currently doing is on each function of each controller I am creating below:
Code:
$seo = array('title' => 'Title of Site', 'keywords' => 'Keywords', 'description' => 'Something about the site');

and when I call the view I call the $seo like:
Code:
$this->load->view('main/main_index', $seo);

Is this the right way to do this? I have a page where I have products and title needs to be dynamic and I don't know what to do there.

Basically if someone can explain to me what is the proper method of doing this, would appreciate it.
#2

[eluser]InsiteFX[/eluser]
Code:
private $data = array();

$data['title']       = 'Title of Site';
$data['keywords']    = 'Keywords';
$data['description'] = 'Something about the site';

// add anything else you need like above.
$data['some_more']   = 'some thing else here';

$this->load->vars($data);

$this->load->view('main/main_index');

InsiteFX
#3

[eluser]xerosis[/eluser]
Thanks.
So Basically this is the right approach and I have to send the private $data in every single function through the controller?

I thought there may be a way that all the metadata handling can be done separately in another file.

Thanks.
#4

[eluser]Julio Fagundes[/eluser]
Code:
Class MY_Controller extends CI_Controller
{

public $seo;

function __construct()
{
    parent::__construct();
    $this->load->model('seo_model');
    $page = $this->router->fetch_class(); // get name of the class (Page)
    $this->seo = $this->seo_model->get_seo_for_page($page); // Array with (title,desc....)
}

}





Code:
Class Home extends MY_Controller
{

   function __construct()
   {
     parent::__construct();
   }

   function index()
   {
      $this->load->vars($this->seo);
      $data['other_data'] = 'foo';
      $this->load->view('main/main_index',$data);
   }

}


I dont tested, but I think that way works fine, test and tell us!

:cheese:
#5

[eluser]InsiteFX[/eluser]
Julio Fagundes,

Your method will give you a big performace hit! Becuase it will have to access the database everytime it switches pages!

InsiteFX
#6

[eluser]Julio Fagundes[/eluser]
I Agree, but just only showing the logic.

Another way:

Code:
Class MY_Controller extends CI_Controller
{

public $seo;

function __construct()
{
    parent::__construct();
    $this->seo = $this->_get_seo_for_pages(); // Array with (title,desc....)
}

function _get_seo_for_pages()
{

   $seo = array(
             'page_especific' => array(
                                    'title' => 'Title of Site',
                                    'keywords' => 'Keywords',
                                    'description' => 'Something about the site'),
             'default' => array(
                                    'title' => 'Title of Site',
                                    'keywords' => 'Keywords',
                                    'description' => 'Something about the site')
            );


    $page = $this->router->fetch_class();
    return (isset($seo[$page])) ? $seo[$page] : $seo['default'];
}

}




Theme © iAndrew 2016 - Forum software by © MyBB