HandlebarsHandlebars

Handlebars template engine, based on mustache.

Defined (2)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Handlebars.php  
  1. class Handlebars 
  2. private static $_instance = false; 
  3. const VERSION = '1.0.0'; 
  4.  
  5. /** 
  6. * factory method 
  7. * @param array $options see __construct's options parameter 
  8. * @return Handlebars 
  9. */ 
  10. public static function factory($options = array()) 
  11. if (self::$_instance === false) { 
  12. self::$_instance = new Handlebars($options); 
  13.  
  14. return self::$_instance; 
  15.  
  16. /** 
  17. * @var Tokenizer 
  18. */ 
  19. private $_tokenizer; 
  20.  
  21. /** 
  22. * @var Parser 
  23. */ 
  24. private $_parser; 
  25.  
  26. /** 
  27. * @var Helpers 
  28. */ 
  29. private $_helpers; 
  30.  
  31. /** 
  32. * @var Loader 
  33. */ 
  34. private $_loader; 
  35.  
  36. /** 
  37. * @var Loader 
  38. */ 
  39. private $_partialsLoader; 
  40.  
  41. /** 
  42. * @var Cache 
  43. */ 
  44. private $_cache; 
  45.  
  46. /** 
  47. * @var callable escape function to use 
  48. */ 
  49. private $_escape = 'htmlspecialchars'; 
  50.  
  51. /** 
  52. * @var array parametes to pass to escape function 
  53. */ 
  54. private $_escapeArgs = array( 
  55. ENT_COMPAT,  
  56. 'UTF-8' 
  57. ); 
  58.  
  59. private $_aliases = array(); 
  60.  
  61. /** 
  62. * Handlebars engine constructor 
  63. * $options array can contain : 
  64. * helpers => Helpers object 
  65. * escape => a callable function to escape values 
  66. * escapeArgs => array to pass as extra parameter to escape function 
  67. * loader => Loader object 
  68. * partials_loader => Loader object 
  69. * cache => Cache object 
  70. * @param array $options array of options to set 
  71. * @throws \InvalidArgumentException 
  72. */ 
  73. public function __construct(array $options = array()) 
  74. if (isset($options['helpers'])) { 
  75. $this->setHelpers($options['helpers']); 
  76.  
  77. if (isset($options['loader'])) { 
  78. $this->setLoader($options['loader']); 
  79.  
  80. if (isset($options['partials_loader'])) { 
  81. $this->setPartialsLoader($options['partials_loader']); 
  82.  
  83. if (isset($options['cache'])) { 
  84. $this->setCache($options['cache']); 
  85.  
  86. if (isset($options['escape'])) { 
  87. if (!is_callable($options['escape'])) { 
  88. throw new \InvalidArgumentException( 
  89. 'Handlebars Constructor "escape" option must be callable' 
  90. ); 
  91.  
  92. $this->_escape = $options['escape']; 
  93.  
  94. if (isset($options['escapeArgs'])) { 
  95. if (!is_array($options['escapeArgs'])) { 
  96. $options['escapeArgs'] = array($options['escapeArgs']); 
  97. $this->_escapeArgs = $options['escapeArgs']; 
  98.  
  99. if (isset($options['partials_alias']) 
  100. && is_array($options['partials_alias']) 
  101. ) { 
  102. $this->_aliases = $options['partials_alias']; 
  103.  
  104.  
  105. /** 
  106. * Shortcut 'render' invocation. 
  107. * Equivalent to calling `$handlebars->loadTemplate($template)->render($data);` 
  108. * @param string $template template name 
  109. * @param mixed $data data to use as context 
  110. * @return string Rendered template 
  111. * @see Handlebars::loadTemplate 
  112. * @see Template::render 
  113. */ 
  114. public function render($template, $data) 
  115. return $this->loadTemplate($template)->render($data); 
  116.  
  117. /** 
  118. * Set helpers for current enfine 
  119. * @param Helpers $helpers handlebars helper 
  120. * @return void 
  121. */ 
  122. public function setHelpers(Helpers $helpers) 
  123. $this->_helpers = $helpers; 
  124.  
  125. /** 
  126. * Get helpers, or create new one if ther is no helper 
  127. * @return Helpers 
  128. */ 
  129. public function getHelpers() 
  130. if (!isset($this->_helpers)) { 
  131. $this->_helpers = new Helpers(); 
  132.  
  133. return $this->_helpers; 
  134.  
  135. /** 
  136. * Add a new helper. 
  137. * @param string $name helper name 
  138. * @param mixed $helper helper callable 
  139. * @return void 
  140. */ 
  141. public function addHelper($name, $helper) 
  142. $this->getHelpers()->add($name, $helper); 
  143.  
  144. /** 
  145. * Get a helper by name. 
  146. * @param string $name helper name 
  147. * @return callable Helper 
  148. */ 
  149. public function getHelper($name) 
  150. return $this->getHelpers()->__get($name); 
  151.  
  152. /** 
  153. * Check whether this instance has a helper. 
  154. * @param string $name helper name 
  155. * @return boolean True if the helper is present 
  156. */ 
  157. public function hasHelper($name) 
  158. return $this->getHelpers()->has($name); 
  159.  
  160. /** 
  161. * Remove a helper by name. 
  162. * @param string $name helper name 
  163. * @return void 
  164. */ 
  165. public function removeHelper($name) 
  166. $this->getHelpers()->remove($name); 
  167.  
  168. /** 
  169. * Set current loader 
  170. * @param Loader $loader handlebars loader 
  171. * @return void 
  172. */ 
  173. public function setLoader(Loader $loader) 
  174. $this->_loader = $loader; 
  175.  
  176. /** 
  177. * get current loader 
  178. * @return Loader 
  179. */ 
  180. public function getLoader() 
  181. if (!isset($this->_loader)) { 
  182. $this->_loader = new StringLoader(); 
  183.  
  184. return $this->_loader; 
  185.  
  186. /** 
  187. * Set current partials loader 
  188. * @param Loader $loader handlebars loader 
  189. * @return void 
  190. */ 
  191. public function setPartialsLoader(Loader $loader) 
  192. $this->_partialsLoader = $loader; 
  193.  
  194. /** 
  195. * get current partials loader 
  196. * @return Loader 
  197. */ 
  198. public function getPartialsLoader() 
  199. if (!isset($this->_partialsLoader)) { 
  200. $this->_partialsLoader = new StringLoader(); 
  201.  
  202. return $this->_partialsLoader; 
  203.  
  204. /** 
  205. * Set cache for current engine 
  206. * @param Cache $cache handlebars cache 
  207. * @return void 
  208. */ 
  209. public function setCache(Cache $cache) 
  210. $this->_cache = $cache; 
  211.  
  212. /** 
  213. * Get cache 
  214. * @return Cache 
  215. */ 
  216. public function getCache() 
  217. if (!isset($this->_cache)) { 
  218. $this->_cache = new Dummy(); 
  219.  
  220. return $this->_cache; 
  221.  
  222. /** 
  223. * Get current escape function 
  224. * @return callable 
  225. */ 
  226. public function getEscape() 
  227. return $this->_escape; 
  228.  
  229. /** 
  230. * Set current escape function 
  231. * @param callable $escape function 
  232. * @throws \InvalidArgumentException 
  233. * @return void 
  234. */ 
  235. public function setEscape($escape) 
  236. if (!is_callable($escape)) { 
  237. throw new \InvalidArgumentException( 
  238. 'Escape function must be a callable' 
  239. ); 
  240. $this->_escape = $escape; 
  241.  
  242. /** 
  243. * Get current escape function 
  244. * @return array 
  245. */ 
  246. public function getEscapeArgs() 
  247. return $this->_escapeArgs; 
  248.  
  249. /** 
  250. * Set current escape function 
  251. * @param array $escapeArgs arguments to pass as extra arg to function 
  252. * @return void 
  253. */ 
  254. public function setEscapeArgs($escapeArgs) 
  255. if (!is_array($escapeArgs)) { 
  256. $escapeArgs = array($escapeArgs); 
  257. $this->_escapeArgs = $escapeArgs; 
  258.  
  259.  
  260. /** 
  261. * Set the Handlebars Tokenizer instance. 
  262. * @param Tokenizer $tokenizer tokenizer 
  263. * @return void 
  264. */ 
  265. public function setTokenizer(Tokenizer $tokenizer) 
  266. $this->_tokenizer = $tokenizer; 
  267.  
  268. /** 
  269. * Get the current Handlebars Tokenizer instance. 
  270. * If no Tokenizer instance has been explicitly specified, this method will 
  271. * instantiate and return a new one. 
  272. * @return Tokenizer 
  273. */ 
  274. public function getTokenizer() 
  275. if (!isset($this->_tokenizer)) { 
  276. $this->_tokenizer = new Tokenizer(); 
  277.  
  278. return $this->_tokenizer; 
  279.  
  280. /** 
  281. * Set the Handlebars Parser instance. 
  282. * @param Parser $parser parser object 
  283. * @return void 
  284. */ 
  285. public function setParser(Parser $parser) 
  286. $this->_parser = $parser; 
  287.  
  288. /** 
  289. * Get the current Handlebars Parser instance. 
  290. * If no Parser instance has been explicitly specified, this method will 
  291. * instantiate and return a new one. 
  292. * @return Parser 
  293. */ 
  294. public function getParser() 
  295. if (!isset($this->_parser)) { 
  296. $this->_parser = new Parser(); 
  297.  
  298. return $this->_parser; 
  299.  
  300. /** 
  301. * Load a template by name with current template loader 
  302. * @param string $name template name 
  303. * @return Template 
  304. */ 
  305. public function loadTemplate($name) 
  306. $source = $this->getLoader()->load($name); 
  307. $tree = $this->_tokenize($source); 
  308.  
  309. return new Template($this, $tree, $source); 
  310.  
  311. /** 
  312. * Load a partial by name with current partial loader 
  313. * @param string $name partial name 
  314. * @return Template 
  315. */ 
  316. public function loadPartial($name) 
  317. if (isset($this->_aliases[$name])) { 
  318. $name = $this->_aliases[$name]; 
  319. $source = $this->getPartialsLoader()->load($name); 
  320. $tree = $this->_tokenize($source); 
  321.  
  322. return new Template($this, $tree, $source); 
  323.  
  324. /** 
  325. * Register partial alias 
  326. * @param string $alias Partial alias 
  327. * @param string $content The real value 
  328. * @return void 
  329. */ 
  330. public function registerPartial($alias, $content) 
  331. $this->_aliases[$alias] = $content; 
  332.  
  333. /** 
  334. * Un-register partial alias 
  335. * @param string $alias Partial alias 
  336. * @return void 
  337. */ 
  338. public function unRegisterPartial($alias) 
  339. if (isset($this->_aliases[$alias])) { 
  340. unset($this->_aliases[$alias]); 
  341.  
  342. /** 
  343. * Load string into a template object 
  344. * @param string $source string to load 
  345. * @return Template 
  346. */ 
  347. public function loadString($source) 
  348. $tree = $this->_tokenize($source); 
  349.  
  350. return new Template($this, $tree, $source); 
  351.  
  352. /** 
  353. * try to tokenize source, or get them from cache if available 
  354. * @param string $source handlebars source code 
  355. * @return array handlebars parsed data into array 
  356. */ 
  357. private function _tokenize($source) 
  358. $hash = md5(sprintf('version: %s, data : %s', self::VERSION, $source)); 
  359. $tree = $this->getCache()->get($hash); 
  360. if ($tree === false) { 
  361. $tokens = $this->getTokenizer()->scan($source); 
  362. $tree = $this->getParser()->parse($tokens); 
  363. $this->getCache()->set($hash, $tree); 
  364.  
  365. return $tree; 
  366.  
/vendor/xamin/handlebars.php/src/Handlebars/Handlebars.php  
  1. class Handlebars 
  2. private static $_instance = false; 
  3. const VERSION = '1.0.0'; 
  4.  
  5. /** 
  6. * factory method 
  7. * @param array $options see __construct's options parameter 
  8. * @return Handlebars 
  9. */ 
  10. public static function factory($options = array()) 
  11. if (self::$_instance === false) { 
  12. self::$_instance = new Handlebars($options); 
  13.  
  14. return self::$_instance; 
  15.  
  16. /** 
  17. * @var Tokenizer 
  18. */ 
  19. private $_tokenizer; 
  20.  
  21. /** 
  22. * @var Parser 
  23. */ 
  24. private $_parser; 
  25.  
  26. /** 
  27. * @var Helpers 
  28. */ 
  29. private $_helpers; 
  30.  
  31. /** 
  32. * @var Loader 
  33. */ 
  34. private $_loader; 
  35.  
  36. /** 
  37. * @var Loader 
  38. */ 
  39. private $_partialsLoader; 
  40.  
  41. /** 
  42. * @var Cache 
  43. */ 
  44. private $_cache; 
  45.  
  46. /** 
  47. * @var string the class to use for the template 
  48. */ 
  49. private $_templateClass = 'Handlebars\\Template'; 
  50.  
  51. /** 
  52. * @var callable escape function to use 
  53. */ 
  54. private $_escape = 'htmlspecialchars'; 
  55.  
  56. /** 
  57. * @var array parametes to pass to escape function 
  58. */ 
  59. private $_escapeArgs = array( 
  60. ENT_COMPAT,  
  61. 'UTF-8' 
  62. ); 
  63.  
  64. private $_aliases = array(); 
  65.  
  66. /** 
  67. * Handlebars engine constructor 
  68. * $options array can contain : 
  69. * helpers => Helpers object 
  70. * escape => a callable function to escape values 
  71. * escapeArgs => array to pass as extra parameter to escape function 
  72. * loader => Loader object 
  73. * partials_loader => Loader object 
  74. * cache => Cache object 
  75. * template_class => the class to use for the template object 
  76. * @param array $options array of options to set 
  77. * @throws \InvalidArgumentException 
  78. */ 
  79. public function __construct(array $options = array()) 
  80. if (isset($options['helpers'])) { 
  81. $this->setHelpers($options['helpers']); 
  82.  
  83. if (isset($options['loader'])) { 
  84. $this->setLoader($options['loader']); 
  85.  
  86. if (isset($options['partials_loader'])) { 
  87. $this->setPartialsLoader($options['partials_loader']); 
  88.  
  89. if (isset($options['cache'])) { 
  90. $this->setCache($options['cache']); 
  91.  
  92. if (isset($options['template_class'])) { 
  93. $this->setTemplateClass($options['template_class']); 
  94.  
  95. if (isset($options['escape'])) { 
  96. if (!is_callable($options['escape'])) { 
  97. throw new \InvalidArgumentException( 
  98. 'Handlebars Constructor "escape" option must be callable' 
  99. ); 
  100.  
  101. $this->_escape = $options['escape']; 
  102.  
  103. if (isset($options['escapeArgs'])) { 
  104. if (!is_array($options['escapeArgs'])) { 
  105. $options['escapeArgs'] = array($options['escapeArgs']); 
  106. $this->_escapeArgs = $options['escapeArgs']; 
  107.  
  108. if (isset($options['partials_alias']) 
  109. && is_array($options['partials_alias']) 
  110. ) { 
  111. $this->_aliases = $options['partials_alias']; 
  112.  
  113.  
  114. /** 
  115. * Shortcut 'render' invocation. 
  116. * Equivalent to calling `$handlebars->loadTemplate($template)->render($data);` 
  117. * @param string $template template name 
  118. * @param mixed $data data to use as context 
  119. * @return string Rendered template 
  120. * @see Handlebars::loadTemplate 
  121. * @see Template::render 
  122. */ 
  123. public function render($template, $data) 
  124. return $this->loadTemplate($template)->render($data); 
  125.  
  126. /** 
  127. * Set helpers for current enfine 
  128. * @param Helpers $helpers handlebars helper 
  129. * @return void 
  130. */ 
  131. public function setHelpers(Helpers $helpers) 
  132. $this->_helpers = $helpers; 
  133.  
  134. /** 
  135. * Get helpers, or create new one if ther is no helper 
  136. * @return Helpers 
  137. */ 
  138. public function getHelpers() 
  139. if (!isset($this->_helpers)) { 
  140. $this->_helpers = new Helpers(); 
  141.  
  142. return $this->_helpers; 
  143.  
  144. /** 
  145. * Add a new helper. 
  146. * @param string $name helper name 
  147. * @param mixed $helper helper callable 
  148. * @return void 
  149. */ 
  150. public function addHelper($name, $helper) 
  151. $this->getHelpers()->add($name, $helper); 
  152.  
  153. /** 
  154. * Get a helper by name. 
  155. * @param string $name helper name 
  156. * @return callable Helper 
  157. */ 
  158. public function getHelper($name) 
  159. return $this->getHelpers()->__get($name); 
  160.  
  161. /** 
  162. * Check whether this instance has a helper. 
  163. * @param string $name helper name 
  164. * @return boolean True if the helper is present 
  165. */ 
  166. public function hasHelper($name) 
  167. return $this->getHelpers()->has($name); 
  168.  
  169. /** 
  170. * Remove a helper by name. 
  171. * @param string $name helper name 
  172. * @return void 
  173. */ 
  174. public function removeHelper($name) 
  175. $this->getHelpers()->remove($name); 
  176.  
  177. /** 
  178. * Set current loader 
  179. * @param Loader $loader handlebars loader 
  180. * @return void 
  181. */ 
  182. public function setLoader(Loader $loader) 
  183. $this->_loader = $loader; 
  184.  
  185. /** 
  186. * get current loader 
  187. * @return Loader 
  188. */ 
  189. public function getLoader() 
  190. if (!isset($this->_loader)) { 
  191. $this->_loader = new StringLoader(); 
  192.  
  193. return $this->_loader; 
  194.  
  195. /** 
  196. * Set current partials loader 
  197. * @param Loader $loader handlebars loader 
  198. * @return void 
  199. */ 
  200. public function setPartialsLoader(Loader $loader) 
  201. $this->_partialsLoader = $loader; 
  202.  
  203. /** 
  204. * get current partials loader 
  205. * @return Loader 
  206. */ 
  207. public function getPartialsLoader() 
  208. if (!isset($this->_partialsLoader)) { 
  209. $this->_partialsLoader = new StringLoader(); 
  210.  
  211. return $this->_partialsLoader; 
  212.  
  213. /** 
  214. * Set cache for current engine 
  215. * @param Cache $cache handlebars cache 
  216. * @return void 
  217. */ 
  218. public function setCache(Cache $cache) 
  219. $this->_cache = $cache; 
  220.  
  221. /** 
  222. * Get cache 
  223. * @return Cache 
  224. */ 
  225. public function getCache() 
  226. if (!isset($this->_cache)) { 
  227. $this->_cache = new Dummy(); 
  228.  
  229. return $this->_cache; 
  230.  
  231. /** 
  232. * Get current escape function 
  233. * @return callable 
  234. */ 
  235. public function getEscape() 
  236. return $this->_escape; 
  237.  
  238. /** 
  239. * Set current escape function 
  240. * @param callable $escape function 
  241. * @throws \InvalidArgumentException 
  242. * @return void 
  243. */ 
  244. public function setEscape($escape) 
  245. if (!is_callable($escape)) { 
  246. throw new \InvalidArgumentException( 
  247. 'Escape function must be a callable' 
  248. ); 
  249. $this->_escape = $escape; 
  250.  
  251. /** 
  252. * Get current escape function 
  253. * @return array 
  254. */ 
  255. public function getEscapeArgs() 
  256. return $this->_escapeArgs; 
  257.  
  258. /** 
  259. * Set current escape function 
  260. * @param array $escapeArgs arguments to pass as extra arg to function 
  261. * @return void 
  262. */ 
  263. public function setEscapeArgs($escapeArgs) 
  264. if (!is_array($escapeArgs)) { 
  265. $escapeArgs = array($escapeArgs); 
  266. $this->_escapeArgs = $escapeArgs; 
  267.  
  268.  
  269. /** 
  270. * Set the Handlebars Tokenizer instance. 
  271. * @param Tokenizer $tokenizer tokenizer 
  272. * @return void 
  273. */ 
  274. public function setTokenizer(Tokenizer $tokenizer) 
  275. $this->_tokenizer = $tokenizer; 
  276.  
  277. /** 
  278. * Get the current Handlebars Tokenizer instance. 
  279. * If no Tokenizer instance has been explicitly specified, this method will 
  280. * instantiate and return a new one. 
  281. * @return Tokenizer 
  282. */ 
  283. public function getTokenizer() 
  284. if (!isset($this->_tokenizer)) { 
  285. $this->_tokenizer = new Tokenizer(); 
  286.  
  287. return $this->_tokenizer; 
  288.  
  289. /** 
  290. * Set the Handlebars Parser instance. 
  291. * @param Parser $parser parser object 
  292. * @return void 
  293. */ 
  294. public function setParser(Parser $parser) 
  295. $this->_parser = $parser; 
  296.  
  297. /** 
  298. * Get the current Handlebars Parser instance. 
  299. * If no Parser instance has been explicitly specified, this method will 
  300. * instantiate and return a new one. 
  301. * @return Parser 
  302. */ 
  303. public function getParser() 
  304. if (!isset($this->_parser)) { 
  305. $this->_parser = new Parser(); 
  306.  
  307. return $this->_parser; 
  308.  
  309. /** 
  310. * Sets the class to use for the template object 
  311. * @param string $class the class name 
  312. * @return void 
  313. */ 
  314. public function setTemplateClass($class) 
  315. if (!is_a($class, 'Handlebars\\Template', true)) { 
  316. throw new \InvalidArgumentException( 
  317. 'Custom template class must extend Template' 
  318. ); 
  319.  
  320. $this->_templateClass = $class; 
  321.  
  322. /** 
  323. * Load a template by name with current template loader 
  324. * @param string $name template name 
  325. * @return Template 
  326. */ 
  327. public function loadTemplate($name) 
  328. $source = $this->getLoader()->load($name); 
  329. $tree = $this->_tokenize($source); 
  330.  
  331. return new $this->_templateClass($this, $tree, $source); 
  332.  
  333. /** 
  334. * Load a partial by name with current partial loader 
  335. * @param string $name partial name 
  336. * @return Template 
  337. */ 
  338. public function loadPartial($name) 
  339. if (isset($this->_aliases[$name])) { 
  340. $name = $this->_aliases[$name]; 
  341. $source = $this->getPartialsLoader()->load($name); 
  342. $tree = $this->_tokenize($source); 
  343.  
  344. return new $this->_templateClass($this, $tree, $source); 
  345.  
  346. /** 
  347. * Register partial alias 
  348. * @param string $alias Partial alias 
  349. * @param string $content The real value 
  350. * @return void 
  351. */ 
  352. public function registerPartial($alias, $content) 
  353. $this->_aliases[$alias] = $content; 
  354.  
  355. /** 
  356. * Un-register partial alias 
  357. * @param string $alias Partial alias 
  358. * @return void 
  359. */ 
  360. public function unRegisterPartial($alias) 
  361. if (isset($this->_aliases[$alias])) { 
  362. unset($this->_aliases[$alias]); 
  363.  
  364. /** 
  365. * Load string into a template object 
  366. * @param string $source string to load 
  367. * @return Template 
  368. */ 
  369. public function loadString($source) 
  370. $tree = $this->_tokenize($source); 
  371.  
  372. return new $this->_templateClass($this, $tree, $source); 
  373.  
  374. /** 
  375. * try to tokenize source, or get them from cache if available 
  376. * @param string $source handlebars source code 
  377. * @return array handlebars parsed data into array 
  378. */ 
  379. private function _tokenize($source) 
  380. $hash = md5(sprintf('version: %s, data : %s', self::VERSION, $source)); 
  381. $tree = $this->getCache()->get($hash); 
  382. if ($tree === false) { 
  383. $tokens = $this->getTokenizer()->scan($source); 
  384. $tree = $this->getParser()->parse($tokens); 
  385. $this->getCache()->set($hash, $tree); 
  386.  
  387. return $tree; 
  388.