| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Mustache.php. |
| 5 |
* |
| 6 |
* (c) 2010-2014 Justin Hileman |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Abstract Mustache Template class. |
| 14 |
* |
| 15 |
* @abstract |
| 16 |
*/ |
| 17 |
abstract class Mustache_Template |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var Mustache_Engine |
| 21 |
*/ |
| 22 |
protected $mustache; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var boolean |
| 26 |
*/ |
| 27 |
protected $strictCallables = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* Mustache Template constructor. |
| 31 |
* |
| 32 |
* @param Mustache_Engine $mustache |
| 33 |
*/ |
| 34 |
public function __construct(Mustache_Engine $mustache) |
| 35 |
{ |
| 36 |
$this->mustache = $mustache; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Mustache Template instances can be treated as a function and rendered by simply calling them: |
| 41 |
* |
| 42 |
* $m = new Mustache_Engine; |
| 43 |
* $tpl = $m->loadTemplate('Hello, {{ name }}!'); |
| 44 |
* echo $tpl(array('name' => 'World')); // "Hello, World!" |
| 45 |
* |
| 46 |
* @see Mustache_Template::render |
| 47 |
* |
| 48 |
* @param mixed $context Array or object rendering context (default: array()) |
| 49 |
* |
| 50 |
* @return string Rendered template |
| 51 |
*/ |
| 52 |
public function __invoke($context = array()) |
| 53 |
{ |
| 54 |
return $this->render($context); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Render this template given the rendering context. |
| 59 |
* |
| 60 |
* @param mixed $context Array or object rendering context (default: array()) |
| 61 |
* |
| 62 |
* @return string Rendered template |
| 63 |
*/ |
| 64 |
public function render($context = array()) |
| 65 |
{ |
| 66 |
return $this->renderInternal($this->prepareContextStack($context)); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Internal rendering method implemented by Mustache Template concrete subclasses. |
| 71 |
* |
| 72 |
* This is where the magic happens :) |
| 73 |
* |
| 74 |
* NOTE: This method is not part of the Mustache.php public API. |
| 75 |
* |
| 76 |
* @param Mustache_Context $context |
| 77 |
* @param string $indent (default: '') |
| 78 |
* |
| 79 |
* @return string Rendered template |
| 80 |
*/ |
| 81 |
abstract public function renderInternal(Mustache_Context $context, $indent = ''); |
| 82 |
|
| 83 |
/** |
| 84 |
* Tests whether a value should be iterated over (e.g. in a section context). |
| 85 |
* |
| 86 |
* In most languages there are two distinct array types: list and hash (or whatever you want to call them). Lists |
| 87 |
* should be iterated, hashes should be treated as objects. Mustache follows this paradigm for Ruby, Javascript, |
| 88 |
* Java, Python, etc. |
| 89 |
* |
| 90 |
* PHP, however, treats lists and hashes as one primitive type: array. So Mustache.php needs a way to distinguish |
| 91 |
* between between a list of things (numeric, normalized array) and a set of variables to be used as section context |
| 92 |
* (associative array). In other words, this will be iterated over: |
| 93 |
* |
| 94 |
* $items = array( |
| 95 |
* array('name' => 'foo'), |
| 96 |
* array('name' => 'bar'), |
| 97 |
* array('name' => 'baz'), |
| 98 |
* ); |
| 99 |
* |
| 100 |
* ... but this will be used as a section context block: |
| 101 |
* |
| 102 |
* $items = array( |
| 103 |
* 1 => array('name' => 'foo'), |
| 104 |
* 'banana' => array('name' => 'bar'), |
| 105 |
* 42 => array('name' => 'baz'), |
| 106 |
* ); |
| 107 |
* |
| 108 |
* @param mixed $value |
| 109 |
* |
| 110 |
* @return boolean True if the value is 'iterable' |
| 111 |
*/ |
| 112 |
protected function isIterable($value) |
| 113 |
{ |
| 114 |
if (is_object($value)) { |
| 115 |
return $value instanceof Traversable; |
| 116 |
} elseif (is_array($value)) { |
| 117 |
$i = 0; |
| 118 |
foreach ($value as $k => $v) { |
| 119 |
if ($k !== $i++) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return true; |
| 125 |
} else { |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Helper method to prepare the Context stack. |
| 132 |
* |
| 133 |
* Adds the Mustache HelperCollection to the stack's top context frame if helpers are present. |
| 134 |
* |
| 135 |
* @param mixed $context Optional first context frame (default: null) |
| 136 |
* |
| 137 |
* @return Mustache_Context |
| 138 |
*/ |
| 139 |
protected function prepareContextStack($context = null) |
| 140 |
{ |
| 141 |
$stack = new Mustache_Context; |
| 142 |
|
| 143 |
$helpers = $this->mustache->getHelpers(); |
| 144 |
if (!$helpers->isEmpty()) { |
| 145 |
$stack->push($helpers); |
| 146 |
} |
| 147 |
|
| 148 |
if (!empty($context)) { |
| 149 |
$stack->push($context); |
| 150 |
} |
| 151 |
|
| 152 |
return $stack; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Resolve a context value. |
| 157 |
* |
| 158 |
* Invoke the value if it is callable, otherwise return the value. |
| 159 |
* |
| 160 |
* @param mixed $value |
| 161 |
* @param Mustache_Context $context |
| 162 |
* @param string $indent |
| 163 |
* |
| 164 |
* @return string |
| 165 |
*/ |
| 166 |
protected function resolveValue($value, Mustache_Context $context, $indent = '') |
| 167 |
{ |
| 168 |
if (($this->strictCallables ? is_object($value) : !is_string($value)) && is_callable($value)) { |
| 169 |
return $this->mustache |
| 170 |
->loadLambda((string) call_user_func($value)) |
| 171 |
->renderInternal($context, $indent); |
| 172 |
} |
| 173 |
|
| 174 |
return $value; |
| 175 |
} |
| 176 |
} |
| 177 |
|