Welcome Guest, Not a member yet? Register   Sign In
Html Helper in Meta tag
#3

(11-06-2014, 04:29 AM)Rufnex Wrote: You can easy extend the html function as described at

https://ellislab.com/codeigniter/user-gu...lpers.html

For you example you have to create a new file called MY_html_helper.php in the "/application/helper" directory with the following content:

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! function_exists('meta'))
{
/**
* Generates meta tags from an array of key/values
*
* @param    array
* @param    string
* @param    string
* @param    string
* @return    string
*/
    function meta($name = '', $content = '', $type = 'name', $newline = "\n")
    {
        // Since we allow the data to be passes as a string, a simple array
        // or a multidimensional one, we need to do a little prepping.
        if ( ! is_array($name))
        {
            $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
        }
        elseif (isset($name['name']))
        {
            // Turn single array into multidimensional
            $name = array($name);
        }

        $str = '';
        foreach ($name as $meta)
        {
            $type        = (isset($meta['type']) && $meta['type'] !== 'name')    ? $meta['type'] : 'name';
            $name        = isset($meta['name'])                    ? $meta['name'] : '';
            $newline    = isset($meta['newline'])                ? $meta['newline'] : "\n";

            $content    = (isset($meta['content']) && !empty($meta['content'])) ? 'content="'.$meta['content'].'" /' : '';

            $str .= '<meta '.$type.'="'.$name.'" '.$content.'>'.$newline;
        }

        return $str;
    }
}

Now you can us this helper like that:

Code:
$this->load->helper('html');
echo meta('utf8', '', 'charset');

Should this code be rolled into the main Codeigniter system, so everyone has access?
Reply


Messages In This Thread
Html Helper in Meta tag - by mark - 11-06-2014, 12:09 AM
RE: Html Helper in Meta tag - by Rufnex - 11-06-2014, 04:29 AM
RE: Html Helper in Meta tag - by Chroma - 11-08-2014, 09:13 AM
RE: Html Helper in Meta tag - by sv3tli0 - 11-08-2014, 01:05 PM
RE: Html Helper in Meta tag - by Chroma - 11-14-2014, 08:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB