If Only I Knew That!

Webby things that could save you time

Archive for July, 2009

Adding Meta data to a codeigniter view from a controller

with 2 comments

To make adding meta data to a view easier, in the controller add to your method

$meta = array (
			'meta_title' 			=> 'title here',
			'meta_keywords' 		=> 'keywords here',
			'meta_description' 		=> 'description here',
			'meta_robots' 			=> 'all',
			'extra_headers' 		=> '
				    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
				    <script type="text/javascript">
				     	google.load("jquery", "1.3.2");

		$data = array();
		// add any data

		// merge meta and data
		$data = array_merge($data,$meta);

		// load view with data
		$this->load->view('pages/home', $data);

The extra header allows adding additional JS and CSS to the specific view

In your view add the following head code

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?php if(isset($meta_title)) echo $meta_title; ?></title>
    <meta name="keywords" content="<?php if(isset($meta_keywords)) echo $meta_keywords; ?>" />
    <meta name="description" content="<?php if(isset($meta_description)) echo $meta_description; ?>" />
    <meta name="robots" content="<?php if(isset($meta_robots)) echo $meta_robots; ?>" />
    <?php if(isset($extra_headers)) echo $extra_headers; ?>

Written by daveganley

July 26th, 2009 at 3:00 pm

Posted in Codeigniter

Tagged with

Powered by CDN Rewrites