Search code examples
phpzend-frameworkzend-layout

Add scripts to overall layout from within a controller


I'm using Zend_view/Zend_Layout but i want to be able to append scripts to the overall template depending on the controller, so within the controller i could do something like:

public function someAction()
{
    $something->headScript()->appendFile('script.js','text/javascript');
    // etc etc
}

I have enabled Zend_view/Zend_Layout like this:

in application.ini:

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

and in Bootstrap.php:

protected function _initView()
{
    $view = new Zend_View();
    $view->doctype('XHTML1_STRICT');
    $view->headTitle('zend layout tester');
    // Add it to the ViewRenderer
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
        'ViewRenderer'
    );
    $viewRenderer->setView($view); 
    // Return it, so that it can be stored by the bootstrap
    return $view;
}

Solution

  • Does this work?

    public function someAction()
    {
        $this->view->headScript()->appendFile('script.js','text/javascript');
        // etc etc
    }