Zend bootstrap, including javascript files for specific pages only

I upload javascript files to bootstrap as usual, but there is a file that I want to include only if it is a page with a form

->appendFile('http://myurl.com/js/formscript.js');

Is there a way to detect a loading page from bootstrap so that I can decide whether to include this file?

I was thinking about passing a variable from form to view, and then checking this variable in bootstrap, but it does not work.

That would be in my form

$layout = new Zend_Layout();
$view = $layout->getView();     
$view->formscript = true; 

and it will be in my bootstrap

if ($view->formscript)

but var_dump($view->formscript)give me null, so any other ideas to activate js files only under certain conditions?

+3
source share
4 answers

It is possible, but you do not need your bootstrap. You can simply access the variable from your layout:

//form
$view = Zend_Layout::getMvcInstance()->getView();
$view->formscript = TRUE;

//layout
if($this->formscript)
{
  $this->headScript()->appendFile('http://myurl.com/js/formscript.js');
}
echo $this->headScript();

getView() , , . , > . >

+3

javascript , ( - *.phtml).

<?php

 $this->headScript()->appendFile('http://myurl.com/js/formscript.js');

?>

, CSS , .

<?php
 $this->headLink()->appendStylesheet('http://myurl.com/styles.css');
?>
+4

- - $view->hasForm - . , , , , .

, - , , , , .

Then your script view or layout can call $this->headScript()->appendFile()if the flag is set.

+1
source

Why not navigate through appendFile()to the form class (of course, if you use Zend_Form), you will be sure that your JS line will be created only at the same time as your form. The location for this line is good in init()as well as inrender()

class Your_Form extends Zend_Form {
    public init(){
        $this->getView()->appendFile('http://myurl.com/js/formscript.js');
        [...]
    }
}
+1
source

All Articles