This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7878403
commit d0b1427
Showing
5 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.kpf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
class IncludeHelperAppController extends Controller {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
class IncludeHelperAppModel extends Model {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |