Permalink
Browse files

Feature tweaks and ehancements

context.php:
    Allow accessing first, second or third elements from an array
    Access does not depend on array keys being [0,1,2,3...]
filters.php:
    Access second or third index from items
    Added json filter
    Limiting words could sometimes cut an html tag in half
tags.php:
    Allow start index in {% for %} tag
  • Loading branch information...
1 parent 93eee9c commit c8a879f73869d3b7cdd131729f042ad2e053f59f @IJMacD committed May 3, 2012
Showing with 137 additions and 117 deletions.
  1. +44 −37 h2o/context.php
  2. +52 −42 h2o/filters.php
  3. +41 −38 h2o/tags.php
View
@@ -9,21 +9,21 @@ class H2o_Context implements ArrayAccess {
public $scopes;
public $options;
public $autoescape = true;
-
+
private $arrayMethods = array('first'=> 0, 'last'=> 1, 'length'=> 2, 'size'=> 3);
static $lookupTable = array();
-
+
function __construct($context = array(), $options = array()){
if (is_object($context))
$context = get_object_vars($context);
$this->scopes = array($context);
-
- if (isset($options['safeClass']))
+
+ if (isset($options['safeClass']))
$this->safeClass = array_merge($this->safeClass, $options['safeClass']);
-
- if (isset($options['autoescape']))
+
+ if (isset($options['autoescape']))
$this->autoescape = $options['autoescape'];
-
+
$this->options = $options;
}
@@ -54,13 +54,13 @@ function offsetGet($key) {
}
return;
}
-
+
function offsetSet($key, $value) {
if (strpos($key, '.') > -1)
throw new Exception('cannot set non local variable');
return $this->scopes[0][$key] = $value;
}
-
+
function offsetUnset($key) {
foreach ($this->scopes as $layer) {
if (isset($layer[$key])) unset($layer[$key]);
@@ -83,11 +83,11 @@ function isDefined($key) {
return $this->offsetExists($key);
}
/**
- *
- *
- *
+ *
+ *
+ *
* Variable name
- *
+ *
* @param $var variable name or array(0 => variable name, 'filters' => filters array)
* @return unknown_type
*/
@@ -96,15 +96,15 @@ function resolve($var) {
# if $var is array - it contains filters to apply
$filters = array();
if ( is_array($var) ) {
-
+
$name = array_shift($var);
$filters = isset($var['filters'])? $var['filters'] : array();
-
- }
+
+ }
else $name = $var;
-
+
$result = null;
-
+
# Lookup basic types, null, boolean, numeric and string
# Variable starts with : (:users.name) to short-circuit lookup
if ($name[0] === ':') {
@@ -116,12 +116,12 @@ function resolve($var) {
}
elseif ($name === 'false') {
$result = false;
- }
+ }
elseif (preg_match('/^-?\d+(\.\d+)?$/', $name, $matches)) {
$result = isset($matches[1])? floatval($name) : intval($name);
}
elseif (preg_match('/^"([^"\\\\]*(?:\\.[^"\\\\]*)*)"|' .
- '\'([^\'\\\\]*(?:\\.[^\'\\\\]*)*)\'$/', $name)) {
+ '\'([^\'\\\\]*(?:\\.[^\'\\\\]*)*)\'$/', $name)) {
$result = stripcslashes(substr($name, 1, -1));
}
}
@@ -131,7 +131,7 @@ function resolve($var) {
$result = $this->applyFilters($result,$filters);
return $result;
}
-
+
function getVariable($name) {
# Local variables. this gives as a bit of performance improvement
if (!strpos($name, '.'))
@@ -147,7 +147,14 @@ function getVariable($name) {
if (isset($object[$part])) {
$object = $object[$part];
} elseif ($part === 'first') {
- $object = isset($object[0])?$object[0]:null;
+ $o = array_slice($object, 0, 1);
@speedmax

speedmax Jun 28, 2012

While the rest of the commits is great, I don't think second and third attribute s a good idea, may be a array[2] like interface has better value

{{ items[2] }}

If you update the pull request without the (second, third) and I am more than happy to merge it.

+ $object = count($o) ? $o[0] : null;
+ } elseif ($part === 'second') {
+ $o = array_slice($object, 1, 1);
+ $object = count($o) ? $o[0] : null;
+ } elseif ($part === 'third') {
+ $o = array_slice($object, 2, 1);
+ $object = count($o) ? $o[0] : null;
} elseif ($part === 'last') {
$last = count($object)-1;
$object = isset($object[$last])?$object[$last]:null;
@@ -161,7 +168,7 @@ function getVariable($name) {
if (isset($object->$part))
$object = $object->$part;
elseif (is_callable(array($object, $part))) {
- $methodAllowed = in_array(get_class($object), $this->safeClass) ||
+ $methodAllowed = in_array(get_class($object), $this->safeClass) ||
(isset($object->h2o_safe) && (
$object->h2o_safe === true || in_array($part, $object->h2o_safe)
)
@@ -176,12 +183,12 @@ function getVariable($name) {
}
function applyFilters($object, $filters) {
-
+
foreach ($filters as $filter) {
$name = substr(array_shift($filter), 1);
$args = $filter;
-
- if (isset(h2o::$filters[$name])) {
+
+ if (isset(h2o::$filters[$name])) {
foreach ($args as $i => $argument) {
# name args
if (is_array($argument)) {
@@ -201,26 +208,26 @@ function applyFilters($object, $filters) {
}
function escape($value, $var) {
-
+
$safe = false;
$filters = (is_array($var) && isset($var['filters']))? $var['filters'] : array();
foreach ( $filters as $filter ) {
-
+
$name = substr(array_shift($filter), 1);
$safe = !$safe && ($name === 'safe');
-
+
$escaped = $name === 'escape';
}
-
+
$should_escape = $this->autoescape || isset($escaped) && $escaped;
-
+
if ( ($should_escape && !$safe)) {
$value = htmlspecialchars($value);
- }
-
+ }
+
return $value;
- }
+ }
function externalLookup($name) {
if (!empty(self::$lookupTable)) {
@@ -238,7 +245,7 @@ class BlockContext {
var $h2o_safe = array('name', 'depth', 'super');
var $block, $index;
private $context;
-
+
function __construct($block, $context, $index) {
$this->block =& $block;
$this->context = $context;
@@ -256,9 +263,9 @@ function depth() {
function super() {
$stream = new StreamWriter;
$this->block->parent->render($this->context, $stream, $this->index+1);
- return $stream->close();
+ return $stream->close();
}
-
+
function __toString() {
return "[BlockContext : {$this->block->name}, {$this->block->filename}]";
}
Oops, something went wrong.

0 comments on commit c8a879f

Please sign in to comment.