Using a helper inside another helper

Published on and tagged with cakephp  helper

If you write a helper you sometimes want to access functions of another helper. Let’s say you want to use the link() function of the HtmlHelper in your own helper. First you have to define the helper you want to use in the $helpers array:

class TestHelper extends AppHelper {
    var $helpers = array('Html');
}

You may expect that you could use the helper with $html->link(), but that leads to an “undefined variable” error (I blunder into this trap almost every time I use this functionality *g*). Instead you have to use $this->Html->link() as shown in the example:

class TestHelper extends AppHelper {
    var $helpers = array('Html');

    function getTheLink() {
        return $this->Html->link('The link', '/');
    }
}

13 comments baked

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License