Skip to content
This repository has been archived by the owner on Mar 2, 2021. It is now read-only.

Commit

Permalink
added project files
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanaelkane committed Sep 1, 2009
1 parent 7878403 commit d0b1427
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.kpf
29 changes: 29 additions & 0 deletions README
@@ -0,0 +1,29 @@
## CakePHP Include Helper Plugin

A simple helper for automagically including css & js assets (PHP 5 only)

### Example usage:

Extract the plugin to your CakePHP plugin directory, eg. .../project/app/plugins/include\_helper/

Setup your app\_controller.php (eg. .../project/app/app\_controller.php)

<?php
class AppController extends Controller {
public $helpers = array('Html', 'Form', 'Javascript', 'IncludeHelper.Include');
}
?>

Use in your layout: (eg. .../project/app/views/layouts/default.ctp)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?= $title_for_layout?></title>
<?= $html->css('default') ?>
<?= $include->includeAssets() ?>
</head>
<body>
<?= $content_for_layout ?>
</body>
</html>
2 changes: 2 additions & 0 deletions include_helper/include_helper_app_controller.php
@@ -0,0 +1,2 @@
<?php
class IncludeHelperAppController extends Controller {}
2 changes: 2 additions & 0 deletions include_helper/include_helper_app_model.php
@@ -0,0 +1,2 @@
<?php
class IncludeHelperAppModel extends Model {}
141 changes: 141 additions & 0 deletions include_helper/views/helpers/include.php
@@ -0,0 +1,141 @@
<?php
/**
* A simple helper for automagically including css & javascript assets
*
* TODO:
* - add some instructions to this file, explaining how to use it
*
* @author Nathanael kane
*/
class IncludeHelper extends Helper {
/**
* Other helpers used by this helper
*
* @var array
* @access public
*/
public $helpers = array('Html', 'Javascript');

/**
* The name of the shared files to look for
*
* @example
* - /css/posts/shared.css
* - /js/posts/shared.js
*
* @var string
* @access private
*/
private $sharedFileName = 'shared';

/**
* The current action, will be modified if the current controller is pages
*
* @example
* Example 1 (normal controller/action use):
* - when URL is '/posts/index' will return 'index'
*
* Example 2 (when using built-in pages controller):
* - when URL is '/pages/home' will return 'display_home'
*
* @var string
* @access private
*/
private $currentAction;

/**
* Automagically include css & javascript for the current controller &
* action
*
* Will include the following js/css files if they exist:
* - /css/<controller>/<sharedFileName>.css
* - /css/<controller>/<currentAction>.css
* - /js/<controller>/<sharedFileName>.js
* - /js/<controller>/<currentAction>.css
*
* @return string Generated css & javascript includes
* @access public
*/
public function includeAssets() {
$this->currentAction = $this->getCurrentAction();
$includes = $this->includeCss();
$includes .= $this->includeJavascript();
return $this->output($includes);
}

/**
* Automagically include css for the current controller & action
*
* Will include the following css files if they exist:
* - /css/<controller>/<sharedFileName>.css
* - /css/<controller>/<currentAction>.css
*
* @return string Generated css includes
* @access private
*/
private function includeCss() {
$includes = $this->checkAndIncludeCss($this->sharedFileName);
$includes .= $this->checkAndIncludeCss($this->currentAction);
return $includes;
}

/**
* Automagically include javascript for the current controller & action
*
* Will include the following js files if they exist:
* - /js/<controller>/<sharedFileName>.js
* - /js/<controller>/<currentAction>.js
*
* @return string Generated javascript includes
* @access private
*/
private function includeJavascript() {
$includes = $this->checkAndIncludeJavascript($this->sharedFileName);
$includes .= $this->checkAndIncludeJavascript($this->currentAction);
return $includes;
}

/**
* Check if a css file exists, include it if it does
*
* @param string $filePath The absolute path of the file to check/include
* @return string Generated css include
* @access private
*/
private function checkAndIncludeCss($file) {
$cssPath = APP . DS . 'webroot' . DS . 'css' . DS . $this->params['controller'] . DS;
if (is_file($cssPath . $file . '.css')) {
return $this->Html->css($this->params['controller'] . '/' . $file, null, array('media' => 'screen, projection'));
}
}

/**
* Check if a css file exists, include it if it does
*
* @param string $filePath The absolute path of the file to check/include
* @return string Generated javascript include
* @access private
*/
private function checkAndIncludeJavascript($file) {
$jsPath = APP . DS . 'webroot' . DS . 'js' . DS . $this->params['controller'] . DS;
if (is_file($jsPath . $file . '.js')) {
return $this->Javascript->link($this->params['controller'] . '/'. $file);
}
}

/**
* Gets the current action
*
* Will append the first param if using the built-in pages controller
*
* @return string The current action
* @access private
*/
private function getCurrentAction() {
$action = $this->params['action'];
if ($this->params['controller'] == 'pages' && !empty($this->params['pass'][0])) {
$action = $action . '_' . $this->params['pass'][0];
}
return $action;
}
}

0 comments on commit d0b1427

Please sign in to comment.