Search code examples
phpzend-frameworkbootstrappingzend-view

How to make variable that can be used in many views?


I have a project, it has a structure like this:

http://img526.imageshack.us/img526/2333/92348689.png

I want to make a variable like the following

$templatePath = $this->baseUrl('/application/templates/')` 

and it can be used in many views, in many modules. I think I can do it by declaring the variable in Bootstrap.php (application) but I don't know how to do that.


Solution

  • Usually, I just place myself such variables into application bootstrap file. Here's an example:

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
        protected function _initViewVariables() {
            $this->bootstrap('View');
    
            $this->view->fooValue = 'foo';
            $this->view->barValue = 'bar';
    
            $config = Zend_Registry::get('config');
    
            if( $config->recaptcha->enabled ) {
                $this->view->captcha = true;
                $this->view->recaptchaPublicKey = $config->recaptcha->publicKey;
            }
            else {
                $this->view->captcha = false;
            }
    
        }
    
    }
    

    I hope it helps!