Welcome Guest, Not a member yet? Register   Sign In
A different heading()
#1

[eluser]superwad[/eluser]
Hello,

I'm new to CI, and eager to learn. I'm building my first site in CI and I came across an instance where a default helper function didn't do what I wanted, so I made a slight change and thought I'd share it with the rest of you.

It's a simple modification of the heading function that takes an optional third parameter as an array that allows attributes to be passed, such as id, class, or whatever. I needed it for my site, and thought somebody else might benefit Smile

Here you go.

Code:
/**
* Heading
*
* Generates an HTML heading tag.  First param is the data.
* Second param is the size of the heading tag.
* Third param is an array of attributes.
*
* @access    public
* @param    string
* @param    integer
* @param       array
* @return    string
*/
if ( ! function_exists('heading'))
{
    function heading($data = '', $h = '1', $attributes=null)
    {
        $out = "<h".$h;
        if (is_array($attributes)) {
            foreach ($attributes as $k=>$v) {
                $out .= " ".$k."=\"".$v."\"";
            }
        }
        $out .= ">".$data."</h".$h.">";
        return $out;
    }
}
#2

[eluser]xwero[/eluser]
Why not create 2 generic tag functions?
Code:
function container_tag($name,$content='',$attributes='')
{
    $out = '<'.$name;
    if (is_array($attributes)) {
            foreach ($attributes as $k=>$v) {
                $out .= " ".$k."=\"".$v."\"";
            }
        }
    return $out.">".$content."</".$name.">";
}

function empty_tag($name,$attributes='')
{
    $out = '<'.$name;
    if (is_array($attributes)) {
            foreach ($attributes as $k=>$v) {
                $out .= " ".$k."=\"".$v."\"";
            }
        }
    return $out.">";
}
I rather hardcode the html because the use of functions to generate html often require more typing.
#3

[eluser]superwad[/eluser]
Well that's OK too. I just amended an existing function to have the attribute support that most other HTML helper functions have.
#4

[eluser]Dam1an[/eluser]
Am I missing something, as I really don't see the purpose of using a PHP function to create a heading, as in almost all cases, the only thing thast changes, is the text, so you just pass that as a variable
Code:
<h1>&lt;?=heading?&gt;</h1>
#5

[eluser]superwad[/eluser]
It was already in the HTML helper to begin with. I'm not creating anything new here, just adding on to.

If you're petitioning to have the function removed from the helper, this probably isn't the right area to voice your concerns. I'm just trying to share a modified core function.
#6

[eluser]Dam1an[/eluser]
[quote author="superwad" date="1240670166"]It was already in the HTML helper to begin with. I'm not creating anything new here, just adding on to.

If you're petitioning to have the function removed from the helper, this probably isn't the right area to voice your concerns. I'm just trying to share a modified core function.[/quote]

I'm not saying it should be removed, I just don't see the appeal of such a helper (nothing personal Wink)

I guess I also prefer to have my views to be more verbose in terms of HTML, so I'd rather see
Code:
<h1>My Heading</h1>
instead of
&lt;?=heading('My Heading', 'h1')?&gt;

Also, but not using the heading function, I manage to avoid loading the helper functions




Theme © iAndrew 2016 - Forum software by © MyBB