Created
January 9, 2012 18:57
-
-
Save airways/1584356 to your computer and use it in GitHub Desktop.
Core tags in Cell CMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* @package CELL CMS | |
* @author Isaac Raway (MetaSushi, LLC) <[email protected]> | |
* | |
* Copyright (c)2009, 2010, 2011. Isaac Raway and MetaSushi, LLC. | |
* All rights reserved. | |
**/ | |
class Core_mod { | |
var $CE_LABEL = 'Core'; | |
var $CE_DESCRIPTION = 'Provides database connection, hooks, modules system, and view handling for Cell CMS.'; | |
var $CE_VERSION = '1.0'; | |
public function __construct() | |
{ | |
$this->CI = &get_instance(); | |
} | |
/* | |
* Single Template Tags | |
*/ | |
public function tag_include($node) | |
{ | |
$template = $node->param('template'); | |
if(!$template) show_error('{ce:core:include} requires a "template" parameter'); | |
return $this->CI->ce_template->apply($template, array($node->tmp_params)); | |
} | |
public function tag_random($node) | |
{ | |
$len = $node->param('len', 5); | |
return random_string('alnum', $len); | |
} | |
/* | |
* Pair Template Tags | |
*/ | |
public function pair_loop($node) | |
{ | |
$times = $node->param('times', 1); | |
$result = ''; | |
for($i = 0; $i < $times; $i++) | |
{ | |
$data['loop'] = $i+1; | |
$result .= $this->CI->ce_template->apply($node, array(array_merge($node->tmp_params, $data)), 0); | |
} | |
return $result; | |
} | |
public function pair_foreach($node) | |
{ | |
$this->CI->load->helper('krumo'); | |
$in = $node->param('in'); | |
//echo gettype($in); | |
//krumo($in); | |
if(!is_array($in)) show_error('{ce:core:foreach} requires an "in" parameter which must be an array'); | |
$count = 0; | |
$result = ''; | |
foreach($in as $key => $data) | |
{ | |
if(is_object($data)) $data = $this->CI->ce_template->to_array($data); | |
$count ++; | |
if(!is_array($data)) | |
{ | |
// if this is still not an array, the value is a scalar and we want to setup a couple default | |
// vars for looping over these kinds of simple arrays | |
$data = array( | |
'loop_key' => $key, | |
'loop_value' => $data | |
); | |
} else { | |
$data['loop_key'] = $key; | |
$data['loop_value'] = $data; | |
} | |
$data['loop'] = $count; | |
$data['loop_last'] = ($count == count($in)); | |
$result .= $this->CI->ce_template->apply($node, array(array_merge($node->tmp_params, $data)), 0); | |
} | |
return $result; | |
} | |
public function pair_recurse($node) | |
{ | |
$in = $node->param('in'); | |
$children_field = $node->param('children_field', '__children'); | |
if(!is_array($in)) show_error('{ce:core:recurse} requires an "in" parameter which must be an array'); | |
$item = array( | |
$children_field => $in | |
); | |
return $this->_recurse($node, $item, $children_field); | |
} | |
private function _recurse(&$node, &$item, &$children_field) | |
{ | |
$count = 0; | |
$result = ''; | |
if(!isset($item[$children_field]) || !$item[$children_field]) return ''; | |
// find the {loop_items}...{/loop_items} pair | |
$loop_items_index = FALSE; | |
for($i = 0; $i < count($node->blocks[0]); $i++) | |
{ | |
if($node->blocks[0][$i]->type == NODE_TAG | |
&& $node->blocks[0][$i]->text == 'loop_items') | |
{ | |
$loop_items_index = $i; | |
break; | |
} | |
} | |
$item['loop_items'] = $item[$children_field]; | |
$item[$children_field] = ''; | |
foreach($item['loop_items'] as $key => $data) | |
{ | |
$count ++; | |
$data = $this->CI->ce_template->to_array($data); | |
if($loop_items_index !== FALSE) | |
{ | |
// render the nested markup to replace the children array | |
$data[$children_field] = $this->_recurse($node->blocks[0][$loop_items_index], array_merge($node->tmp_params, $data), $children_field); | |
} else { | |
// no loop tag found, so we can't render nested markup - this whole tag won't work very well | |
// without this but we don't want to cause an error | |
$data[$children_field] = ''; | |
} | |
$item['loop_items'][$key] = $data; | |
} | |
//var_dump($item['loop_items'][0]); | |
// apply block to this item | |
$result = $this->CI->ce_template->apply($node, array(array_merge($node->tmp_params, $item)), 0); | |
return $result; | |
} | |
function pair_trunc($node) | |
{ | |
$len = $node->param('len', 150); | |
$strip_html = $node->param('strip_html', TRUE, 'bool'); | |
$append = $node->param('append', '...'); | |
$result = $this->CI->ce_template->apply($node, array($node->tmp_params), 0); | |
if($strip_html) $result = strip_tags($result); | |
if(strlen($result) > $len) | |
{ | |
$result = substr($result, 0, $len); | |
$result .= $append; | |
} | |
return $result; | |
} | |
/* | |
* Template functions | |
*/ | |
function func_len(&$stack) | |
{ | |
$x = array_pop($stack); | |
return strlen($x); | |
} | |
function func_keys(&$stack) | |
{ | |
$x = array_pop($stack); | |
return array_keys($x); | |
} | |
function func_contains(&$stack) | |
{ | |
$y = array_pop($stack); | |
$x = array_pop($stack); | |
return array_search($y, $x) !== FALSE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment