Quantcast

custom html helper

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

custom html helper

thomas.mery@gmail.com

Hi all,

I want to have a custom HTML Helper in my helpers dir and it seems like
it's always the one belonging to the core that is used, it doesn't seem
to be the same with the form helper for instance.

any idea what i' doing wrong ?

is it an intended behaviour ?

tia

thomas


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: custom html helper

Ryders

Hi Thomas,

I don't know in what order dirs are looked up for helpers, but after
what you said, I would assume the helpers directory from the core is
looked at first. However I ran into the same problem, the html helper
had a few things I wanted to improve and so what I did is creating my
own html helper;

I still didn't figure out how to 'extend' a helper... but I hope this
will help;

First I created a file called eo_html.php in my application helpers
dir. it looks like this; (partial)

<code>
<?php
class eoHtmlHelper extends Helper {
        var $helpers = array('Html');
}
?>
</code>

the var $helpers = array('Html'); line ensures I'll be able to call the
html helper when needed.

Then I whenever I run into something in the html helper that makes no
sense in my app, I 'override' (sort of). For instance the
html->selectTag will not select the default value (from the object)
unless $selected is specified... so instead of modifying the
htmlhelper, I added the following to my helper;

<code>
function selectTag($fieldName, $optionElements, $selected = null,
$selectAttr = array(), $optionAttr = null, $showEmpty = true, $return =
false) {
    if (!$selected) $selected = $this->Html->tagValue($fieldName);
    return $this->Html->setFormTag($fieldName, $optionElements,
$selected, $selectAttr, $optionAttr, $showEmpty, $return);
}
</code>

notice how I call the original html helper with '$this->Html->...'

Finally, in my view, instead of going $html->selectTag, I use
$eoHtml->selectTag, of course after I added eoHtml to my $helpers array
in my controller!

I cannot say that's the best solution ever (and would appreciate any
comment in re to that!), but so far it's worked quite well!

Hope this helps!

Seb.


On Dec 5, 5:47 am, "[hidden email]" <[hidden email]>
wrote:

> Hi all,
>
> I want to have a custom HTML Helper in my helpers dir and it seems like
> it's always the one belonging to the core that is used, it doesn't seem
> to be the same with the form helper for instance.
>
> any idea what i' doing wrong ?
>
> is it an intended behaviour ?
>
> tia
>
> thomas


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

RE: custom html helper

Mariano Iglesias

Extend the helper this way:

1. Create a file called myhtml.php on your app/views/helpers directory.

2. Have its contents be something like the following. I'm overriding method
selectTag as in your example, with some small changes, like... PLEASE USE
BRACES! :)

<?php

loadHelper('html');

class MyhtmlHelper extends HtmlHelper
{
        function selectTag($fieldName, $optionElements, $selected = null,
$selectAttr = array(), $optionAttr = null, $showEmpty = true, $return =
false)
        {
                if (!$selected)
                {
                        $selected = parent::tagValue($fieldName);
                }

                return parent::setFormTag($fieldName, $optionElements,
$selected, $selectAttr, $optionAttr, $showEmpty, $return);
        }
}

?>

So you see whenever you need to call cake's html helper you just do
parent::method()

3. Then on your controllers add the helper:

var $helpers = array ( 'Myhtml' );

4. And use it on your views:

$myhtml->selectTag(...)

-MI

---------------------------------------------------------------------------

Remember, smart coders answer ten questions for every question they ask.
So be smart, be cool, and share your knowledge.

BAKE ON!


-----Mensaje original-----
De: [hidden email] [mailto:[hidden email]] En nombre
de Ryders
Enviado el: Lunes, 04 de Diciembre de 2006 06:35 p.m.
Para: Cake PHP
Asunto: Re: custom html helper

I still didn't figure out how to 'extend' a helper... but I hope this
will help;


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: custom html helper

Ryders

Dude!!

loadHelper('html'); is what I was missing! cheers for that!

Braces... I'll take a good note of it! ;)

Cheers!


Seb.

On Dec 5, 9:04 am, "Mariano Iglesias" <[hidden email]>
wrote:

> Extend the helper this way:
>
> 1. Create a file called myhtml.php on your app/views/helpers directory.
>
> 2. Have its contents be something like the following. I'm overriding method
> selectTag as in your example, with some small changes, like... PLEASE USE
> BRACES! :)
>
> <?php
>
> loadHelper('html');
>
> class MyhtmlHelper extends HtmlHelper
> {
>         function selectTag($fieldName, $optionElements, $selected = null,
> $selectAttr = array(), $optionAttr = null, $showEmpty = true, $return =
> false)
>         {
>                 if (!$selected)
>                 {
>                         $selected = parent::tagValue($fieldName);
>                 }
>
>                 return parent::setFormTag($fieldName, $optionElements,
> $selected, $selectAttr, $optionAttr, $showEmpty, $return);
>         }
>
> }?>
>
> So you see whenever you need to call cake's html helper you just do
> parent::method()
>
> 3. Then on your controllers add the helper:
>
> var $helpers = array ( 'Myhtml' );
>
> 4. And use it on your views:
>
> $myhtml->selectTag(...)
>
> -MI
>
> ---------------------------------------------------------------------------
>
> Remember, smart coders answer ten questions for every question they ask.
> So be smart, be cool, and share your knowledge.
>
> BAKE ON!
>
> -----Mensaje original-----
> De: [hidden email] [mailto:[hidden email]] En nombre
> de Ryders
> Enviado el: Lunes, 04 de Diciembre de 2006 06:35 p.m.
> Para: Cake PHP
> Asunto: Re: custom html helper
>
> I still didn't figure out how to 'extend' a helper... but I hope this
> will help;


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: custom html helper

thomas.mery@gmail.com

thanks to both of you

I really thought if I had a html.php in helpers folder it would be used
instead of the core one like it seems to happen for form.php for
instance ...

if anyone is able to clarify ... :)

tia

thomas


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...