Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend bootstrap, including javascript files for certain pages only

I'm loading javascript files in the bootstrap as usual, but there's a file that I want to have included only if it's a page that has a form

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

Is there a way to detect the page being loaded, from the bootstrap so I can decide whether or not to include this file?

I thought about passing a variable from the Form to the view, and then checking for that variable in the bootstrap, but it's not working.

This would be in my form

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

and this would be in my bootstrap

if ($view->formscript)

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

like image 914
sameold Avatar asked Sep 27 '26 01:09

sameold


2 Answers

To include javascript files in a particular pages alone, add the following code in those pages(I mean view scripts - *.phtml).

<?php

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

?>

Similarly, to add CSS files to a particular page, do the following.

<?php
 $this->headLink()->appendStylesheet('http://myurl.com/styles.css');
?>
like image 90
Lenin Raj Rajasekaran Avatar answered Sep 28 '26 15:09

Lenin Raj Rajasekaran


It is possible, but you do not need your bootstrap. You can just 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();

Do not use getView() in your form as it will return the view object for the form, not for your application. This has tripped me up more than a couple of times >.>

like image 23
Richard Parnaby-King Avatar answered Sep 28 '26 16:09

Richard Parnaby-King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!