mkdir application/views/helpers/ -p && touch application/views/helpers/LoadInlineJs.php
Vim LoadInlineJs.php
, and type in follow code:
<?php
class Zend_View_Helper_LoadInlineJs extends Zend_View_Helper_Abstract
{
function loadInlineJs ($folderPath = "js/application/") {
$request = Zend_Controller_Front::getInstance()->getRequest();
$file_uri = $folderPath . $request->getControllerName() . '/' . $request->getActionName() . '.js';
if (file_exists($file_uri)) {
$this->view->inlineScript()->appendFile('/' . $file_uri, 'text/javascript');
}
}
}
Right before the closing 'body' tag in the layout file(application/layouts/scripts/layout.phtml), put in the code to print the js:
<?php
$this->loadInlineJs() ;
echo( $this -> inlineScript()."\n");
?>
Now create a js file, for example: makdir -p public/js/application/index/ && touch public/js/application/index/index.js
check the source code in a browser, the output before the closing 'body' tag will be sth like:
<div style="display:none;">
<script type="text/javascript" src="/js/application/index/index.js"></script>
</div>
</body>
So basically the rule is to auto load js with src="/js/application/controllerName/actionName.js", if it exists.
And if a customized js needed for sepicified page, the only action need to take it to create the js file based it's controller name and action name.
Add new comment