Skip to content

Instantly share code, notes, and snippets.

@gautamk
Created February 29, 2012 14:25
Show Gist options
  • Save gautamk/1941194 to your computer and use it in GitHub Desktop.
Save gautamk/1941194 to your computer and use it in GitHub Desktop.
Auto include Action Specific Css and Javascript CakePHP
<?php
// Add this to any `view` recommended is View/Layouts/default.ctp
/*
WWW_ROOT generally refers to your webroot directory
DS is usually '/'
$this->params['controller'] returns controller name in lowercase
$this->params['action'] returns action name in lowercase
Refer
* http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html
* http://stackoverflow.com/a/1425219
*/
if (is_file( WWW_ROOT . 'js' . DS . $this->params['controller'] . DS . $this->params['action'] . '.js')) {
echo $this->Html->script($this->params['controller'].DS.$this->params['action']);
}
if (is_file(WWW_ROOT . 'css' . DS . $this->params['controller'] . DS . $this->params['action'] . '.css')) {
echo $this->Html->css($this->params['controller'].DS.$this->params['action']);
}
/*
Where to place JS and CSS files
APP_DIR/webroot/js/<lowercase_controller_name>/<lowercase_action_name>.js
APP_DIR/webroot/css/<lowercase_controller_name>/<lowercase_action_name>.css
Example
APP_DIR/webroot/js/pages/index.js
APP_DIR/webroot/css/pages/index.css
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment