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

As far as meta tag is refurnished ref: http://www.w3schools.com/tags/att_meta_charset.asp
the CI html helper is not updated
Sad
Reply
#2

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');

Reply
#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
#4

At the moment CI is generating HTML 4.1 valid output.
How ever you are right that HTML5 is already on stage.
Perhaps those helpers needs to get an improvement to start supporting different HTML standards.
Only this way we will have support over both HTML5 and HTML4 and all the future versions.
Best VPS Hosting : Digital Ocean
Reply
#5

(11-08-2014, 01:05 PM)sv3tli0 Wrote: At the moment CI is generating HTML 4.1 valid output.
How ever you are right that HTML5 is already on stage.
Perhaps those helpers needs to get an improvement to start supporting different HTML standards.
Only this way we will have support over both HTML5 and HTML4 and all the future versions.

I have moved all my new development to HTML5.

I understand the consequences to this, but it seems that the more people who move forward and keep going forward, the better adoption there will be of the more recent standards.

Besides, most of the people still using 10 year old computers, are not the types to be buying or using new things.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB