From 1dfceeba7a37cdf5ba4eff7e3c85707e5fd33fd5 Mon Sep 17 00:00:00 2001 From: REJack Date: Tue, 7 Nov 2017 01:56:13 +0100 Subject: [PATCH] enhance assets function - add abilty to create group assets - create assets helper to render group assets - added config option `render_all_assets` --- config/twiggy.php | 13 +++++++++++++ helpers/twiggy_helper.php | 23 ++++++++++++++++++++++- libraries/Twiggy.php | 32 ++++++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/config/twiggy.php b/config/twiggy.php index 06322b1..5c7ee36 100644 --- a/config/twiggy.php +++ b/config/twiggy.php @@ -179,6 +179,19 @@ $config['twiggy']['load_twig_engine'] = false; +/* +|-------------------------------------------------------------------------- +| Render Assets incl. Groups +|-------------------------------------------------------------------------- +| +| If you activate this any assets including group asset get rendered +| through {{asset}} in layout/template +| +*/ + +$config['twiggy']['render_all_assets'] = false; + + /* |-------------------------------------------------------------------------- | Syntax Delimiters diff --git a/helpers/twiggy_helper.php b/helpers/twiggy_helper.php index e4f3031..7078ef6 100644 --- a/helpers/twiggy_helper.php +++ b/helpers/twiggy_helper.php @@ -1,4 +1,25 @@ + * @author Raphael "REJack" Jackstadt + * @license http://www.opensource.org/licenses/MIT + * @version 0.9.8 + * @copyright Copyright (c) 2012-2014 Edmundas KondraĊĦovas + * @copyright Copyright (c) 2015-2017 Raphael "REJack" Jackstadt + */ -/* End of file twiggy_helper.php */ \ No newline at end of file +function assets($group = NULL) +{ + $CII =& get_instance(); + return $CII->twiggy->_compile_group_assetdata($group); +} \ No newline at end of file diff --git a/libraries/Twiggy.php b/libraries/Twiggy.php index 3cc63b7..8e79060 100644 --- a/libraries/Twiggy.php +++ b/libraries/Twiggy.php @@ -45,7 +45,7 @@ class Twiggy { private $system_register_functions = array('get_class', 'defined', 'isset', 'realpath', 'strpos', 'debug_backtrace'); - private $system_register_safe_functions = array(); + private $system_register_safe_functions = array('assets'); public function __construct() @@ -316,9 +316,12 @@ public function unset_meta() * @return object instance of this class */ - public function asset($type, $name, $value, $extra=array()) - { - $this->_asset[$name] = array('type' => $type, 'value' => $value, 'extra' => $extra); + public function asset($type, $value, $group=NULL, $extra=array()) + { + if ($group) + $this->_asset[$group][] = array('type' => $type, 'value' => $value, 'extra' => $extra); + else + $this->_asset[] = array('type' => $type, 'value' => $value, 'extra' => $extra); return $this; } @@ -486,6 +489,22 @@ private function _compile_assetdata() return $html; } + /** + * Compile group asset data into pure HTML + * + * @access private + * @return string HTML + */ + + public function _compile_group_assetdata($group) + { + if ( ! isset($this->_asset[$group])) + return; + $html = ''; + foreach ($this->_asset[$group] as $asset) $html .= $this->_asset_to_html($asset); + return $html; + } + /** * Convert meta tag array to HTML code * @@ -515,6 +534,11 @@ private function _meta_to_html($meta) private function _asset_to_html($asset) { + if ( ! isset($asset['type']) && isset($asset[0]) && $this->_config['render_all_assets']) + $asset = $asset[0]; + else + return; + if($asset['type'] == 'script'){ $extra = ''; if(isset($asset['extra'])){