philsturgeon / codeigniter-template

Template library for CodeIgniter which supports modules, themes, partial views, etc.
411 stars 178 forks source link

Exhance set_metadata() for additional elements #14

Open rjdjohnston opened 13 years ago

rjdjohnston commented 13 years ago

Slight modification to set_metadata method:

public function set_metadata($name, $content, $type = 'meta', $extra = FALSE)
{
    $name = htmlspecialchars(strip_tags($name));
    $content = htmlspecialchars(strip_tags($content));

    // Keywords with no comments? ARG! comment them
    if ($name == 'keywords' AND ! strpos($content, ','))
    {
        $content = preg_replace('/[\s]+/', ', ', trim($content));
    }

    $add = '';
    $key = '';
    if ($extra)
    {
        foreach ($extra as $k => $v)
        {
            $add .= $k.'="'.$v.'" ';
            $key = '_'.$k.$v;
        }
    }

    switch($type)
    {
        case 'meta':
            $this->_metadata[$name.$key] = '<meta name="'.$name.'" content="'.$content.'" '.$add.' />';
        break;

        case 'link':
            $this->_metadata[$content.$key] = '<link rel="'.$name.'" href="'.$content.'" '.$add.' />';
        break;
    }

    return $this;
}

Usage: $this->template->set_metadata('apple-touch-icon-precomposed', site_url('/images/apple-touch-icon-precomposed.png'), 'link', array('sizes' => '114x114'));

Sorry, no time to add git and do pull :-/