PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 2.0.6
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v2.0.6
4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 All 340 releases
profile-builder / assets / lib / Mustache / Template.php

Template.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 2.0.6, at assets/lib/Mustache/Template.php

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