Skip to content

Commit 1dfceeb

Browse files
committed
enhance assets function
- add abilty to create group assets - create assets helper to render group assets - added config option `render_all_assets`
1 parent 5122be6 commit 1dfceeb

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

config/twiggy.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@
179179
$config['twiggy']['load_twig_engine'] = false;
180180

181181

182+
/*
183+
|--------------------------------------------------------------------------
184+
| Render Assets incl. Groups
185+
|--------------------------------------------------------------------------
186+
|
187+
| If you activate this any assets including group asset get rendered
188+
| through {{asset}} in layout/template
189+
|
190+
*/
191+
192+
$config['twiggy']['render_all_assets'] = false;
193+
194+
182195
/*
183196
|--------------------------------------------------------------------------
184197
| Syntax Delimiters

helpers/twiggy_helper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
23

4+
/**
5+
* Twiggy - Twig template engine implementation for CodeIgniter
6+
*
7+
* Twiggy is not just a simple implementation of Twig template engine
8+
* for CodeIgniter. It supports themes, layouts, templates for regular
9+
* apps and also for apps that use HMVC (module support).
10+
*
11+
* @package CodeIgniter
12+
* @subpackage Twiggy
13+
* @author (Original Author) Edmundas Kondrašovas <as@edmundask.lt>
14+
* @author Raphael "REJack" Jackstadt <info@rejack.de>
15+
* @license http://www.opensource.org/licenses/MIT
16+
* @version 0.9.8
17+
* @copyright Copyright (c) 2012-2014 Edmundas Kondrašovas <as@edmundask.lt>
18+
* @copyright Copyright (c) 2015-2017 Raphael "REJack" Jackstadt <info@rejack.de>
19+
*/
320

4-
/* End of file twiggy_helper.php */
21+
function assets($group = NULL)
22+
{
23+
$CII =& get_instance();
24+
return $CII->twiggy->_compile_group_assetdata($group);
25+
}

libraries/Twiggy.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Twiggy {
4545

4646
private $system_register_functions = array('get_class', 'defined', 'isset', 'realpath', 'strpos', 'debug_backtrace');
4747

48-
private $system_register_safe_functions = array();
48+
private $system_register_safe_functions = array('assets');
4949

5050

5151
public function __construct()
@@ -316,9 +316,12 @@ public function unset_meta()
316316
* @return object instance of this class
317317
*/
318318

319-
public function asset($type, $name, $value, $extra=array())
320-
{
321-
$this->_asset[$name] = array('type' => $type, 'value' => $value, 'extra' => $extra);
319+
public function asset($type, $value, $group=NULL, $extra=array())
320+
{
321+
if ($group)
322+
$this->_asset[$group][] = array('type' => $type, 'value' => $value, 'extra' => $extra);
323+
else
324+
$this->_asset[] = array('type' => $type, 'value' => $value, 'extra' => $extra);
322325
return $this;
323326
}
324327

@@ -486,6 +489,22 @@ private function _compile_assetdata()
486489
return $html;
487490
}
488491

492+
/**
493+
* Compile group asset data into pure HTML
494+
*
495+
* @access private
496+
* @return string HTML
497+
*/
498+
499+
public function _compile_group_assetdata($group)
500+
{
501+
if ( ! isset($this->_asset[$group]))
502+
return;
503+
$html = '';
504+
foreach ($this->_asset[$group] as $asset) $html .= $this->_asset_to_html($asset);
505+
return $html;
506+
}
507+
489508
/**
490509
* Convert meta tag array to HTML code
491510
*
@@ -515,6 +534,11 @@ private function _meta_to_html($meta)
515534

516535
private function _asset_to_html($asset)
517536
{
537+
if ( ! isset($asset['type']) && isset($asset[0]) && $this->_config['render_all_assets'])
538+
$asset = $asset[0];
539+
else
540+
return;
541+
518542
if($asset['type'] == 'script'){
519543
$extra = '';
520544
if(isset($asset['extra'])){

0 commit comments

Comments
 (0)