Juan Jardim
Juan Jardim

Reputation: 2252

Add an js file depending on the action. Zend

I'm using zend framework to create a web project and I was wondering if there is a way to insert in the template or view a javascript file depending on the type of action. For instance if I had this action:

 public function indexAction() {
        $this->_helper->layout->setLayout("layout_ember");
        //$this->headScript()->appendFile('/scripts/menu.js'); //no work
    }

I want to add the file menu.js to the view/template.

Can you help me with this?

I'm using Zend Framework Version: 1.12.0

Upvotes: 1

Views: 122

Answers (2)

doydoy44
doydoy44

Reputation: 5772

Try this:

In your action :

to add js at the bottom of view

$this->view->InlineScript()->appendFile($this->view->baseUrl() .'/scripts/menu.js'); 

to add js in the header

$this->view->headScript()->appendFile($this->view->baseUrl() .'/scripts/menu.js'); // 

In your Layout For js in head :

<head>
<?php
    echo $this->headScript() ;
?>
 </head>

For js at the bottom :

<?php
    echo $this->InlineScript(); 
?>
</body>

Upvotes: 2

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

This should work

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

Upvotes: 0

Related Questions