robjmills
robjmills

Reputation: 18598

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;
}

Upvotes: 1

Views: 188

Answers (2)

hsz
hsz

Reputation: 152294

Put in layout template

{headScript()}

And in controller's action:

$this->view->headScript()->appendFile('path/to/script.js');

Upvotes: 1

Gordon
Gordon

Reputation: 317177

Does this work?

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

Upvotes: 3

Related Questions