Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'release/1.1'

* release/1.1:
  Fix omission in readme.
  Update readme to document usage of attr argument to render_css and render_js.
  render_css and render_js support an attr argument (defaults to array()). This is passed to the html_tag function, allowing user-defined attributes to be used.
  When a group is rendered, it is disabled. This allows the following: Casset::render_js('specific_group'); ... much laster ... Casset::render_js(); Where the second call to render_js will not render the group 'specific_grou'. If rendering the same group twice is desired, Casset::enable_js('specific_group') can be used between those two calls.
  show_files_inline was being loaded from config incorrectly.
  Different approach to not printing groups with zero files in.
  Fix problem with add_asset where it wasn't correctly creating new groups.
  Update to the readme wrt group creating. When Casset::js() / css() is called with a group which doesn't yet exist, that group is created, and enabled.
  • Loading branch information...
commit 3f004f176fc58ca6830746bda1ca2afaf4dc8b6b 2 parents 1d79dbe + ed09e5a
@canton7 authored
Showing with 47 additions and 25 deletions.
  1. +21 −21 classes/casset.php
  2. +26 −4 readme.md
View
42 classes/casset.php
@@ -124,7 +124,7 @@ public static function _init()
static::$min = Config::get('casset.min', static::$min);
static::$show_files = Config::get('casset.show_files', static::$show_files);
- static::$show_files_inline = Config::get('casset.show_files', static::$show_files_inline);
+ static::$show_files_inline = Config::get('casset.show_files_inline', static::$show_files_inline);
static::$initialized = true;
}
@@ -322,10 +322,7 @@ private static function add_asset($type, $script, $script_min, $group)
if (!array_key_exists($group, static::$groups[$type]))
{
// Assume they want the group enabled
- static::add_group($type, $group, array(
- 'files' => array(array($script, $script_min)),
- 'enabled' => true
- ));
+ static::add_group($type, $group, array(array($script, $script_min)), true);
}
else
{
@@ -376,9 +373,8 @@ private static function add_asset_inline($type, $content)
* @param bool $min True to minify the javascript files. null to use the config value
* @return string The javascript tags to be written to the page
*/
- public function render_js($group = false, $inline = false, $min = null)
+ public function render_js($group = false, $inline = false, $attr = array(), $min = null)
{
- // Very simple minimisation for now
if ($min === null)
$min = static::$min;
@@ -398,24 +394,24 @@ public function render_js($group = false, $inline = false, $min = null)
}, $file_group)).'-->'.PHP_EOL;
}
if ($inline)
- $ret .= html_tag('script', array('type' => 'text/javascript'), PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.'/'.$filename).PHP_EOL).PHP_EOL;
+ $ret .= html_tag('script', array('type' => 'text/javascript')+$attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.'/'.$filename).PHP_EOL).PHP_EOL;
else
$ret .= html_tag('script', array(
'type' => 'text/javascript',
'src' => static::$asset_url.static::$cache_path.$filename,
- ), '').PHP_EOL;
+ )+$attr, '').PHP_EOL;
}
else
{
foreach ($file_group as $file)
{
if ($inline)
- $ret .= html_tag('script', array('type' => 'text/javascript'), PHP_EOL.file_get_contents($file['file']).PHP_EOL).PHP_EOL;
+ $ret .= html_tag('script', array('type' => 'text/javascript')+$attr, PHP_EOL.file_get_contents($file['file']).PHP_EOL).PHP_EOL;
else
$ret .= html_tag('script', array(
'type' => 'text/javascript',
'src' => static::$asset_url.$file['file'],
- ), '').PHP_EOL;
+ )+$attr, '').PHP_EOL;
}
}
@@ -433,7 +429,7 @@ public function render_js($group = false, $inline = false, $min = null)
* @param bool $min True to minify the css files. null to use the config value
* @return string The css tags to be written to the page
*/
- public function render_css($group = false, $inline = false, $min = null)
+ public function render_css($group = false, $inline = false, $attr = array(), $min = null)
{
if ($min === null)
$min = static::$min;
@@ -454,26 +450,26 @@ public function render_css($group = false, $inline = false, $min = null)
}, $file_group)).'-->'.PHP_EOL;
}
if ($inline)
- $ret .= html_tag('style', array('type' => 'text/css'), PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.'/'.$filename).PHP_EOL).PHP_EOL;
+ $ret .= html_tag('style', array('type' => 'text/css')+$attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.'/'.$filename).PHP_EOL).PHP_EOL;
else
$ret .= html_tag('link', array(
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => static::$asset_url.static::$cache_path.$filename,
- )).PHP_EOL;
+ )+$attr).PHP_EOL;
}
else
{
foreach ($file_group as $file)
{
if ($inline)
- $ret .= html_tag('style', array('type' => 'text/css'), PHP_EOL.file_get_contents($file['file']).PHP_EOL).PHP_EOL;
+ $ret .= html_tag('style', array('type' => 'text/css')+$attr, PHP_EOL.file_get_contents($file['file']).PHP_EOL).PHP_EOL;
else
$ret .= html_tag('link', array(
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => static::$asset_url.$file['file'],
- )).PHP_EOL;
+ )+$attr).PHP_EOL;
}
}
@@ -492,10 +488,9 @@ public function render_css($group = false, $inline = false, $min = null)
*/
private static function files_to_render($type, $group, $min)
{
+ // If no group specified, print all groups.
if ($group == false)
- $group_names = array_keys(array_filter(static::$groups[$type], function($a){
- return count($a['files']) > 0;
- }));
+ $group_names = array_keys(static::$groups[$type]);
else
$group_names = array($group);
@@ -507,9 +502,14 @@ private static function files_to_render($type, $group, $min)
{
if (static::$groups[$type][$group_name]['enabled'] == false)
continue;
+ // If there are no files in the group, there's no point in printing it.
+ if (count(static::$groups[$type][$group_name]['files']) == 0)
+ continue;
+
+ $files[$group_name] = array();
- if (!array_key_exists($group_name, $files))
- $files[$group_name] = array();
+ // Mark the group as disabled to avoid the same group being printed twice
+ static::asset_enabled($type, $group_name, false);
foreach (static::$groups[$type][$group_name]['files'] as $file_set)
{
View
30 readme.md
@@ -109,7 +109,8 @@ array element.
Groups can be enabled using `Casset::enable_js('group_name')`, and disabled using `Casset::disable_js('group_name')`. CSS equivalents also exist.
The shortcuts `Casset::enable('group_name')` and `Casset::disable('group_name')` also exist, which will enable/disable both the js and css groups of the given name, if they exist.
-Specific groups can be rendered using `Casset::render_js('group_name')`. If no group name is passed, *all* groups will be rendered.
+Specific groups can be rendered using eg `Casset::render_js('group_name')`. If no group name is passed, *all* groups will be rendered.
+Note that when a group is rendered, it is disabled. See the "Extra attributes" section for an application of this functionality.
Files can be added to a group by passing the group name as the third argument to `Casset::js` / `Casset::css`, eg:
@@ -118,7 +119,8 @@ Casset::js('myfile.js', 'myfile.min.js', 'group_name');
Casset::css('myfile.css', false, 'group_name');
```
-Groups can also be declared on the fly, as shown below. I can't think of why you'd want to do this, which is why the syntax is a little messy.
+Groups can also be declared on the fly, by specifying a group name which doesn't yet exist. The group is assumed to be enabled.
+You can also use a slightly more involved syntax for creating groups, which allows you to specify multiple files and whether the group is enabled, as shown below:
```php
Casset::add_group('js', 'group_name', array('file1.js', array('file2.js', 'file2.min.js')), $enabled);
@@ -167,6 +169,25 @@ Will output:
Similarly, `Casset::css_inline()` and `Casset::render_css_inline()` exist.
+Extra attributes
+----------------
+
+`Casset::render_js()` and `Casset::render_css()` support an optional third argument which allows the user to define extra attributes to be applied to the script/link tag.
+This can be combined with the fact that one a group has been rendered, it is disabled, allowing the following to be done:
+
+```php
+Casset::css('main.css');
+Casset::css('screen.css', false, 'screen');
+
+// Render the 'screen' group
+echo Casset::render_css('screen', false, array('media' => 'screen');
+// <link rel="stylesheet" type="text/css" href="http://...screen.css" media="screen" />
+
+// Render everything else, except the 'screen' group
+echo Casset::render_css();
+// <link rel="stylesheet" type="text/css" href="http://...main.css" media="screen" />
+```
+
Minification
------------
@@ -179,11 +200,11 @@ The assumption is that each group is likely to appear fairly independantly, so c
You can choose to include a comment above each `<script>` and `<link>` tag saying which group is contained with that file by setting the "show_files" key to true in the config file.
Similarly, you can choose to put comments inside each minified file, saying which origin file has ended up where -- set "show_files_inline" to true.
-`Casset::render_js()` and `Casset::render_css()` take an optional third argument, allowing you to control minification on a per-group basis if you need.
+`Casset::render_js()` and `Casset::render_css()` take an optional fourth argument, allowing you to control minification on a per-group basis if you need.
The following will minify the 'group_name' group, even if minification is turned off in the config file.
```php
-echo Casset::render_js('group_name', false, true);
+echo Casset::render_js('group_name', false, array(), true);
```
NOTE: If you change the contents of a group, a new cache file will be generated. However the old one will not be removed (Casset doesn't know if you've got a single page where you add an extra file to a group).
@@ -217,6 +238,7 @@ In the config file:
'files' => array(
'jquery-ui.css',
),
+ 'endabled' => false,
),
),
),
Please sign in to comment.
Something went wrong with that request. Please try again.