Skip to content

Commit

Permalink
enhance assets function
Browse files Browse the repository at this point in the history
 - add abilty to create group assets
 - create assets helper to render group assets
 - added config option `render_all_assets`
  • Loading branch information
REJack committed Nov 7, 2017
1 parent 5122be6 commit 1dfceeb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
13 changes: 13 additions & 0 deletions config/twiggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion helpers/twiggy_helper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* Twiggy - Twig template engine implementation for CodeIgniter
*
* Twiggy is not just a simple implementation of Twig template engine
* for CodeIgniter. It supports themes, layouts, templates for regular
* apps and also for apps that use HMVC (module support).
*
* @package CodeIgniter
* @subpackage Twiggy
* @author (Original Author) Edmundas Kondrašovas <[email protected]>
* @author Raphael "REJack" Jackstadt <[email protected]>
* @license http://www.opensource.org/licenses/MIT
* @version 0.9.8
* @copyright Copyright (c) 2012-2014 Edmundas Kondrašovas <[email protected]>
* @copyright Copyright (c) 2015-2017 Raphael "REJack" Jackstadt <[email protected]>
*/

/* End of file twiggy_helper.php */
function assets($group = NULL)
{
$CII =& get_instance();
return $CII->twiggy->_compile_group_assetdata($group);
}
32 changes: 28 additions & 4 deletions libraries/Twiggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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'])){
Expand Down

0 comments on commit 1dfceeb

Please sign in to comment.