Skip to content

Instantly share code, notes, and snippets.

@berarma
Created December 26, 2013 11:50
Show Gist options
  • Save berarma/8132793 to your computer and use it in GitHub Desktop.
Save berarma/8132793 to your computer and use it in GitHub Desktop.
CakePHP helper class that extends AssetCompress.AssetCompress to inline CSS and JS.
<?php
App::uses('AssetCompress.AssetCompress', 'View/Helper');
class AssetsHelper extends AssetCompressHelper {
public function inlineCss($file, $options = array()) {
if (Configure::read('debug') > 0) {
return $this->css($file, $options);
}
$file = $this->_addExt($file, '.css');
$path = $this->config()->cachePath('css');
$fullFilePath = $path . $this->_getBuildName($file);
$fileContents = file_get_contents($fullFilePath);
return $this->Html->tag('style', $fileContents, array('type' => 'text/css'));
}
public function inlineScript($file, $options = array()) {
if (Configure::read('debug') > 0) {
return $this->script($file, $options);
}
$file = $this->_addExt($file, '.js');
$path = $this->config()->cachePath('js');
$fullFilePath = $path . $this->_getBuildName($file);
$fileContents = file_get_contents($fullFilePath);
return $this->Html->scriptBlock($fileContents);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment