isIterable fits better in Template than Context.
This commit is contained in:
@@ -148,7 +148,7 @@ class Compiler {
|
|||||||
->renderInternal($context, $buffer->getIndent())
|
->renderInternal($context, $buffer->getIndent())
|
||||||
);
|
);
|
||||||
} elseif (!empty($value)) {
|
} elseif (!empty($value)) {
|
||||||
$values = $context->isIterable($value) ? $value : array($value);
|
$values = $this->isIterable($value) ? $value : array($value);
|
||||||
foreach ($values as $value) {
|
foreach ($values as $value) {
|
||||||
$context->push($value);%s
|
$context->push($value);%s
|
||||||
$context->pop();
|
$context->pop();
|
||||||
|
|||||||
@@ -28,52 +28,6 @@ class Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests whether a value should be iterated over (e.g. in a section context).
|
|
||||||
*
|
|
||||||
* In most languages there are two distinct array types: list and hash (or whatever you want to call them). Lists
|
|
||||||
* should be iterated, hashes should be treated as objects. Mustache follows this paradigm for Ruby, Javascript,
|
|
||||||
* Java, Python, etc.
|
|
||||||
*
|
|
||||||
* PHP, however, treats lists and hashes as one primitive type: array. So Mustache.php needs a way to distinguish
|
|
||||||
* between between a list of things (numeric, normalized array) and a set of variables to be used as section context
|
|
||||||
* (associative array). In other words, this will be iterated over:
|
|
||||||
*
|
|
||||||
* $items = array(
|
|
||||||
* array('name' => 'foo'),
|
|
||||||
* array('name' => 'bar'),
|
|
||||||
* array('name' => 'baz'),
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* ... but this will be used as a section context block:
|
|
||||||
*
|
|
||||||
* $items = array(
|
|
||||||
* 1 => array('name' => 'foo'),
|
|
||||||
* 'banana' => array('name' => 'bar'),
|
|
||||||
* 42 => array('name' => 'baz'),
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* @param mixed $value
|
|
||||||
*
|
|
||||||
* @return boolean True if the value is 'iterable'
|
|
||||||
*/
|
|
||||||
public function isIterable($value) {
|
|
||||||
if (is_object($value)) {
|
|
||||||
return $value instanceof \Traversable;
|
|
||||||
} elseif (is_array($value)) {
|
|
||||||
$i = 0;
|
|
||||||
foreach ($value as $k => $v) {
|
|
||||||
if ($k !== $i++) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Push a new Context frame onto the stack.
|
* Push a new Context frame onto the stack.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -72,4 +72,51 @@ abstract class Template {
|
|||||||
* @return string Rendered template
|
* @return string Rendered template
|
||||||
*/
|
*/
|
||||||
abstract public function renderInternal(Context $context);
|
abstract public function renderInternal(Context $context);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether a value should be iterated over (e.g. in a section context).
|
||||||
|
*
|
||||||
|
* In most languages there are two distinct array types: list and hash (or whatever you want to call them). Lists
|
||||||
|
* should be iterated, hashes should be treated as objects. Mustache follows this paradigm for Ruby, Javascript,
|
||||||
|
* Java, Python, etc.
|
||||||
|
*
|
||||||
|
* PHP, however, treats lists and hashes as one primitive type: array. So Mustache.php needs a way to distinguish
|
||||||
|
* between between a list of things (numeric, normalized array) and a set of variables to be used as section context
|
||||||
|
* (associative array). In other words, this will be iterated over:
|
||||||
|
*
|
||||||
|
* $items = array(
|
||||||
|
* array('name' => 'foo'),
|
||||||
|
* array('name' => 'bar'),
|
||||||
|
* array('name' => 'baz'),
|
||||||
|
* );
|
||||||
|
*
|
||||||
|
* ... but this will be used as a section context block:
|
||||||
|
*
|
||||||
|
* $items = array(
|
||||||
|
* 1 => array('name' => 'foo'),
|
||||||
|
* 'banana' => array('name' => 'bar'),
|
||||||
|
* 42 => array('name' => 'baz'),
|
||||||
|
* );
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return boolean True if the value is 'iterable'
|
||||||
|
*/
|
||||||
|
protected function isIterable($value) {
|
||||||
|
if (is_object($value)) {
|
||||||
|
return $value instanceof \Traversable;
|
||||||
|
} elseif (is_array($value)) {
|
||||||
|
$i = 0;
|
||||||
|
foreach ($value as $k => $v) {
|
||||||
|
if ($k !== $i++) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user