-- Ian Warner <[EMAIL PROTECTED]> wrote
(on Monday, 21 January 2008, 12:24 PM +0000):
> However still no joy, this is my stack output for the view - and I assume
> if the new path was registered correctly then it would show up in here
>
<snip>
>
> Registering the path - tried in controller and a few other places.
>
> $view = new Zend_View();
>
> // Add /other/path/to/helpers with class prefix 'Your_View_Helper'
> $view->addHelperPath('D:\AWebEnvironment\htdocs\Site_Template\library\Common\Components',
>
> 'Common_Components');
>
> My helper class taken from the example.
>
> <?php
> class Common_Components_SpecialPurpose
> {
> protected $_count = 0;
>
> public function specialPurpose()
> {
> $this->_count++;
> $output = "I have you {$this->_count} time(s).";
> return htmlspecialchars($output);
> }
> }
The above looks good...
> Ok having debugged the View files in the Framework it seems that I am
> instantiating the View class adding the helpers - below is an output of
> _path
<snip -- path looked good>
> but then the view is getting instantiated again somewhere and killing my
> adding paths with its own SetPath
>
> Same output of _path
>
> [helper] => Array
> (
> [0] => Array
> (
> [prefix] => Zend_View_Helper_
> [dir] => sites\admintrianglesolutions\views\helpers\
> )
>
> [1] => Array
> (
> [prefix] => Zend_View_Helper_
> [dir] =>
> D:\AWebEnvironment\ZendFramework-1.0.3\library\Zend\View\Helper\
> )
>
> )
>
> So its basicalyl getting overwritten later on. Should this happen?
>
> I am not calling new Zend_View anywhere else in my code - so it must be
> when the viewRenderer is automatically instantiated
I see what's going on. Yes, the ViewRenderer instantiates its own view
unless it is provided one -- so if you want to add a global helper path,
you need to tell the ViewRenderer. (The view object is not a singleton,
so if you set the helper path on one instance, it doesn't propagate to
other instances.)
You have a couple of ways of doing this. In your bootstrap, you can
create your view object, and inject it into the ViewRenderer:
$view = new Zend_View();
$view->addHelperPath('path/to/Common/Components/', 'Common_Components');
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')
->setView($view);
(I broke the last statement over two lines to make it readable.)
The next option is to take the view out of the ViewRenderer and alter
it. The method for doing so varies, based on whether the ViewRenderer
will have initialized the view object or not. The ViewRenderer first
initializes its view when its internal init() method is called -- which
is triggered the first time a controller is dispatched.
So, let's say you want to do the above in an early running plugin -- in
which case the view won't have been initialized. Here's one example of
how that might work:
class ViewPlugin extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract
$request)
{
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')
$viewRenderer->initView();
$viewRenderer->view->addHelperPath(
'path/to/Common/Components',
'Common_Components');
}
}
If you wanted to set it from within a controller or an action helper,
the view will have been initialized, and you can just do the following:
$viewRenderer =
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')
$viewRenderer->view->addHelperPath(
'path/to/Common/Components',
'Common_Components');
Hope that helps.
> Matthew Weier O'Phinney wrote:
>> -- Ian Warner <[EMAIL PROTECTED]> wrote
>> (on Sunday, 20 January 2008, 07:54 PM +0000):
>>> $view = new Zend_View();
>>>
>>> // Add /other/path/to/helpers with class prefix
>>> 'Your_View_Helper'
>>>
>>> $view->setHelperPath('D:\AWebEnvironment\htdocs\Site_Template\library\Zend\View\Helper',
>>>
>>> 'Common_Components');
>>>
>>> I have tried the above and also with addHelperPath and still no luck -
>>> tried this on my ubuntu machine also.
>>>
>>> whats the naming convention for helpers, the docs dont give any advice on
>>> what the actual file name should be
>> Not to be mean, but they *do* spell it out, several times:
>> http://framework.zend.com/manual/en/zend.view.helpers.html
>> Second paragraph: "A helper is simply a class. Let's say we want a
>> helper named
>> 'fooBar'. By default, the class is prefixed with 'Zend_View_Helper_'
>> (you can specify a custom prefix when setting a helper path), and
>> the last segment of the class name is the helper name; this segment
>> should be TitleCapped; the full class name is then:
>> Zend_View_Helper_FooBar."
>> And further in that same page:
>> 37.4.3. Writing Custom Helpers
>> Writing custom helpers is easy; just follow these rules:
>> * The class name must, at the very minimum, end with the
>> helper name itself, using CamelCaps. E.g., if you were
>> writing a helper called "specialPurpose", the class name
>> would minimally need to be "SpecialPurpose". You may,
>> and should, give the class name a prefix, and it is
>> recommended that you use 'View_Helper' as part of that
>> prefix: "My_View_Helper_SpecialPurpose". (You will need
>> to pass in the prefix, with or without the trailing
>> underscore, to addHelperPath() or setHelperPath()). There's also one
>> other place it's mentioned, in the section detailing
>> helper paths.
>> So, in your example, if you have the prefix "Common_Components" as your
>> view helper class prefix, and your library code is in
>> "D:\AWebEnvironment\htdocs\Site_Template\Library\', the most logical
>> place to put these helpers would be in the "Common\Components\"
>> subdirectory of that location, and you would then add the following path
>> to your view helper paths:
>>
>> $view->addHelperPath('D:\AWebEnvironment\htdocs\Site_Template\Library\Common\Components\',
>>
>> 'Common_Components');
>> If you then have a "foo" helper, it would be in the 'Foo.php'
>> file in that directory, and have the class name Common_Components_Foo.
>> Hope that helps.
>>> Matthew Weier O'Phinney wrote:
>>>> -- Ian Warner <[EMAIL PROTECTED]> wrote
>>>> (on Sunday, 20 January 2008, 01:00 PM +0000):
>>>>> Sorry I should have said that I tried that also.
>>>>>
>>>>> Still no luck
>>>> I use code like the following code regularly:
>>>> $view->addHelperPath('path/to/My/View/Helper', 'My_View_Helper');
>>>> and it works fine. Can you send me the code you used when trying
>>>> addHelperPath()?
>>>>> Pieter wrote:
>>>>>> Hi Ian,
>>>>>> I think that you should use the $view->addHelperPath() function.
>>>>>> Regards,
>>>>>> Pieter Kokx
>>>>>> Ian Warner schreef:
>>>>>>> hi
>>>>>>>
>>>>>>> I am having real trouble trying to set helper paths.
>>>>>>>
>>>>>>> I assume I can place this anywhere in my code structure - I have
>>>>>>> tried in bootstrap / controllers and a few other places
>>>>>>>
>>>>>>> Pathing - I think I have this correct below though I although I have
>>>>>>> changed this any which way but loose.
>>>>>>>
>>>>>>> $view = new Zend_View();
>>>>>>>
>>>>>>> // Add /other/path/to/helpers with class prefix
>>>>>>> 'Your_View_Helper'
>>>>>>>
>>>>>>> $view->setHelperPath('D:\AWebEnvironment\htdocs\Site_Template\library\Zend\View\Helper',
>>>>>>>
>>>>>>> 'Common_Components');
>>>>>>>
>>>>>>> I have this when I throw the stack in the view.
>>>>>>>
>>>>>>> [_path:private] => Array
>>>>>>> (
>>>>>>> [script] => Array
>>>>>>> (
>>>>>>> [0] =>
>>>>>>> sites\admintrianglesolutions\views\scripts\
>>>>>>> )
>>>>>>>
>>>>>>> [helper] => Array
>>>>>>> (
>>>>>>> [0] => Array
>>>>>>> (
>>>>>>> [prefix] => Zend_View_Helper_
>>>>>>> [dir] =>
>>>>>>> sites\admintrianglesolutions\views\helpers\
>>>>>>> )
>>>>>>>
>>>>>>> [1] => Array
>>>>>>> (
>>>>>>> [prefix] => Zend_View_Helper_
>>>>>>> [dir] =>
>>>>>>> D:\AWebEnvironment\ZendFramework-1.0.3\library\Zend\View\Helper\
>>>>>>> )
>>>>>>>
>>>>>>> )
>>>>>>>
>>>>>>>
>>>>>>>
>
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/