BigJobbies
BigJobbies

Reputation: 4343

CodeIgniter - Displaying and entering dynamic meta data

I have started using CodeIgniter ... Im finding it is quite good, although I have a bit of an issue.

Whats the best way to handle meta data? ... In the views folder, I have created another folder called 'includes' then in there I have added header, footer, nav views.

So I'm taking it that for each controller meta data needs to be entered and then passed to the header view.

If I could get some examples of how you all go about this, that would be fantastic.

Cheers,

Upvotes: 2

Views: 10447

Answers (3)

Oldenborg
Oldenborg

Reputation: 936

I know this is an out of date question, but in case anyone is looking for a simple solution for this in Codeigniter 2.2

I found the simplest thing was to create a helper for this.

Create a file located in config/seo_config.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['seo_title']  = 'My title';
$config['seo_desc']   = 'My description';
$config['seo_robot']  = true;

/* End of file seo_config.php */
/* Location: ./application/config/seo_config.php */

Create a file located in application/helers/seo_helper.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * SEO Helper function
 *
 * Generates Meta tags for SEO
 *
 * @author    Henrik Oldenborg
 * @version   1.0
 */

/**
 * meta_tags() 
 *
 * Generates tags for title, description and robots
 * Using title and description from config file as default
 *
 * @access  public
 * @param   string  Title
 * @param   string  Description (155 characters)
 * @param   bool    Robots follow or no folow
 */

if(! function_exists('meta_tags')){
  function meta_tags($meta)
  {
    $CI =& get_instance();
    $CI->config->load('seo_config');

    if(!isset($meta['title']))
      $meta['title'] = $CI->config->item('seo_title');

    if(!isset($meta['desc']))
      $meta['desc'] = $CI->config->item('seo_desc');

    if(!isset($meta['robot']))
      $meta['robot'] = $CI->config->item('seo_robot');

    $html = '';

    //uses default set in seo_config.php

    $html .= '<title>'.$meta['title'].'</title>';
    $html .= '<meta name="title" content="'.$meta['title'].'"/>';
    $html .= '<meta name="description" content="'.$meta['desc'].'"/>';
    if($meta['robot'] == true){
      $html .= '<meta name="robots" content="index,follow"/>';

    } else {
      $html .= '<meta name="robots" content="noindex,nofollow"/>';
    }
    echo $html;
  }
}

/* End of file seo_helper.php */
/* Location: ./application/helpers/seo_helper.php */

Load up the helper - (Either in the controller or in config/autoload.php)

$this->load->helper('seo_helper');

Add the following code in your view between the two header tags

<?=(isset($meta) ? meta_tags($meta) : meta_tags());?>

Now, all you need to do is declare the $meta variable in your controller like so

$data['meta']['title'] = 'My special title';
$data['meta']['desc'] = 'My special description';
$this->load->view('mytemplate',$data)

Usefull tip You might want to declare your meta data in the controllers constructor. This way you can easily override it when needed in a underlying function, like so.

class Forum extends CI_Controller {

  private $data = array();

  function __construct()
  {
    $this->data['meta']['title'] = 'My forum title';
    $this->data['meta']['robot'] = false;
    parent::__construct();
  }

  public function index()
  {
    $this->data['content'] = 'forum';
    $this->load->view('userarea/template',$this->data);
  }

  public function topic($topic_id)
  {
    $this->data['meta']['title'] = 'My specific title';
    $this->data['content'] = 'topic';
    $this->load->view('userarea/template',$this->data);
  }
}

Upvotes: 2

Brad
Brad

Reputation: 1691

I find its just easiet to pass it right in the head of the page with arrays

    $meta = array(
    array('name' => 'description', 'content' => 'Political website, Liberal, progressive, blog, posts,'),
    array('name' => 'keywords', 'content' => 'politics, McCain, Beck, Hannity, Rush Limbaugh, Environment, Obama, ZB Block, Sarah Palin, Republicans, GOP, Democrats, Liberals, Conservatives, Reagan, Politicususa, FreakOut Nation, Raw Story, Congress, Senate, Representatives, Constitution, White, Black, racial, racsim, blog, blogging, Lemon, Lemonrose, Fox, Fox News,
    political, partys, president'),
    array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'),       
    );
   echo meta($meta); 

Upvotes: -1

tomthorgal
tomthorgal

Reputation: 525

Create a new file in your libraries folder:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View_lib {

  public function __construct()
  {
    $this->CI =& get_instance();
  }

  public function load_view($template, $data = NULL)
  {
    $this->CI->load->view('header', $data);
    $this->CI->load->view($template);
    $this->CI->load->view('footer');
  }

}

/* End of file view_lib.php */
/* Location: ./system/application/libraries/view_lib.php */

Then load this library in your controller:

$this->load->library('view_lib');

Then call your view file in a function like this:

$this->view_lib->load_view('name_of_view_file', $data);

or (if you call a static file without any data to pass):

$this->view_lib->load_view('name_of_view_file');

There are many ways of doing this, but this one works nicely for the applications I am working on. In one of my projects I have multiple functions in the view_lib library to load with or without sidebar, different headers and footers depending if a user is logged in.

Hope this helps, cheers.

Upvotes: 1

Related Questions