HandlebarsHelpers

Handlebars helpers.

Defined (2)

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

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Helpers.php  
  1. class Helpers 
  2. /** 
  3. * @var array array of helpers 
  4. */ 
  5. protected $helpers = array(); 
  6.  
  7. /** 
  8. * Create new helper container class 
  9. * @param array $helpers array of name=>$value helpers 
  10. * @param array|bool $defaults add defaults helper 
  11. * (if, unless, each, with, bindAttr) 
  12. * @throws \InvalidArgumentException when $helpers is not an array 
  13. * (or traversable) or helper is not a callable 
  14. */ 
  15. public function __construct($helpers = null, $defaults = true) 
  16. if ($defaults) { 
  17. $this->addDefaultHelpers(); 
  18. if ($helpers != null) { 
  19. if (!is_array($helpers) && !$helpers instanceof \Traversable) { 
  20. throw new \InvalidArgumentException( 
  21. 'HelperCollection constructor expects an array of helpers' 
  22. ); 
  23. foreach ($helpers as $name => $helper) { 
  24. $this->add($name, $helper); 
  25.  
  26.  
  27. /** 
  28. * Add default helpers (if unless each with bindAttr) 
  29. * @return void 
  30. */ 
  31. protected function addDefaultHelpers() 
  32. $this->add('if', new Helper\IfHelper()); 
  33. $this->add('each', new Helper\EachHelper()); 
  34. $this->add('unless', new Helper\UnlessHelper()); 
  35. $this->add('with', new Helper\WithHelper()); 
  36.  
  37. //Just for compatibility with ember 
  38. $this->add('bindAttr', new Helper\BindAttrHelper()); 
  39.  
  40. /** 
  41. * Add a new helper to helpers 
  42. * @param string $name helper name 
  43. * @param mixed $helper a callable or Helper implementation as a helper 
  44. * @throws \InvalidArgumentException if $helper is not a callable 
  45. * @return void 
  46. */ 
  47. public function add($name, $helper) 
  48. if (!is_callable($helper) && ! $helper instanceof Helper) { 
  49. throw new \InvalidArgumentException( 
  50. "$name Helper is not a callable or doesn't implement the Helper interface." 
  51. ); 
  52. $this->helpers[$name] = $helper; 
  53.  
  54. /** 
  55. * Calls a helper, whether it be a Closure or Helper instance 
  56. * @param string $name The name of the helper 
  57. * @param \Handlebars\Template $template The template instance 
  58. * @param \Handlebars\Context $context The current context 
  59. * @param array $args The arguments passed the the helper 
  60. * @param string $source The source 
  61. * @throws \InvalidArgumentException 
  62. * @return mixed The helper return value 
  63. */ 
  64. public function call($name, Template $template, Context $context, $args, $source) 
  65. if (!$this->has($name)) { 
  66. throw new \InvalidArgumentException('Unknown helper: ' . $name); 
  67.  
  68. if ($this->helpers[$name] instanceof Helper) { 
  69. return $this->helpers[$name]->execute( 
  70. $template, $context, $args, $source 
  71. ); 
  72.  
  73. return call_user_func($this->helpers[$name], $template, $context, $args, $source); 
  74.  
  75. /** 
  76. * Check if $name helper is available 
  77. * @param string $name helper name 
  78. * @return boolean 
  79. */ 
  80. public function has($name) 
  81. return array_key_exists($name, $this->helpers); 
  82.  
  83. /** 
  84. * Get a helper. __magic__ method :) 
  85. * @param string $name helper name 
  86. * @throws \InvalidArgumentException if $name is not available 
  87. * @return callable helper function 
  88. */ 
  89. public function __get($name) 
  90. if (!$this->has($name)) { 
  91. throw new \InvalidArgumentException('Unknown helper :' . $name); 
  92.  
  93. return $this->helpers[$name]; 
  94.  
  95. /** 
  96. * Check if $name helper is available __magic__ method :) 
  97. * @param string $name helper name 
  98. * @return boolean 
  99. * @see Handlebras_Helpers::has 
  100. */ 
  101. public function __isset($name) 
  102. return $this->has($name); 
  103.  
  104. /** 
  105. * Add a new helper to helpers __magic__ method :) 
  106. * @param string $name helper name 
  107. * @param callable $helper a function as a helper 
  108. * @return void 
  109. */ 
  110. public function __set($name, $helper) 
  111. $this->add($name, $helper); 
  112.  
  113. /** 
  114. * Unset a helper 
  115. * @param string $name helper name to remove 
  116. * @return void 
  117. */ 
  118. public function __unset($name) 
  119. $this->remove($name); 
  120.  
  121. /** 
  122. * Check whether a given helper is present in the collection. 
  123. * @param string $name helper name 
  124. * @throws \InvalidArgumentException if the requested helper is not present. 
  125. * @return void 
  126. */ 
  127. public function remove($name) 
  128. if (!$this->has($name)) { 
  129. throw new \InvalidArgumentException('Unknown helper: ' . $name); 
  130.  
  131. unset($this->helpers[$name]); 
  132.  
  133. /** 
  134. * Clear the helper collection. 
  135. * Removes all helpers from this collection 
  136. * @return void 
  137. */ 
  138. public function clear() 
  139. $this->helpers = array(); 
  140.  
  141. /** 
  142. * Check whether the helper collection is empty. 
  143. * @return boolean True if the collection is empty 
  144. */ 
  145. public function isEmpty() 
  146. return empty($this->helpers); 
/vendor/xamin/handlebars.php/src/Handlebars/Helpers.php  
  1. class Helpers 
  2. /** 
  3. * @var array array of helpers 
  4. */ 
  5. protected $helpers = array(); 
  6.  
  7. /** 
  8. * Create new helper container class 
  9. * @param array $helpers array of name=>$value helpers 
  10. * @param array|bool $defaults add defaults helper 
  11. * (if, unless, each, with, bindAttr) 
  12. * @throws \InvalidArgumentException when $helpers is not an array 
  13. * (or traversable) or helper is not a callable 
  14. */ 
  15. public function __construct($helpers = null, $defaults = true) 
  16. if ($defaults) { 
  17. $this->addDefaultHelpers(); 
  18. if ($helpers != null) { 
  19. if (!is_array($helpers) && !$helpers instanceof \Traversable) { 
  20. throw new \InvalidArgumentException( 
  21. 'HelperCollection constructor expects an array of helpers' 
  22. ); 
  23. foreach ($helpers as $name => $helper) { 
  24. $this->add($name, $helper); 
  25.  
  26.  
  27. /** 
  28. * Add default helpers (if unless each with bindAttr) 
  29. * @return void 
  30. */ 
  31. protected function addDefaultHelpers() 
  32. $this->add('if', new Helper\IfHelper()); 
  33. $this->add('each', new Helper\EachHelper()); 
  34. $this->add('unless', new Helper\UnlessHelper()); 
  35. $this->add('with', new Helper\WithHelper()); 
  36.  
  37. //Just for compatibility with ember 
  38. $this->add('bindAttr', new Helper\BindAttrHelper()); 
  39.  
  40. /** 
  41. * Add a new helper to helpers 
  42. * @param string $name helper name 
  43. * @param mixed $helper a callable or Helper implementation as a helper 
  44. * @throws \InvalidArgumentException if $helper is not a callable 
  45. * @return void 
  46. */ 
  47. public function add($name, $helper) 
  48. if (!is_callable($helper) && ! $helper instanceof Helper) { 
  49. throw new \InvalidArgumentException( 
  50. "$name Helper is not a callable or doesn't implement the Helper interface." 
  51. ); 
  52. $this->helpers[$name] = $helper; 
  53.  
  54. /** 
  55. * Add all helpers from the specified collection to the current one. 
  56. * The method will override helpers from the current collections with same 
  57. * named helpers from the specified collection. 
  58. * @param Helpers $helpers A collection which helpers should be added. 
  59. * @return void 
  60. */ 
  61. public function addHelpers(Helpers $helpers) 
  62. $this->helpers = $helpers->getAll() + $this->helpers; 
  63.  
  64. /** 
  65. * Calls a helper, whether it be a Closure or Helper instance 
  66. * @param string $name The name of the helper 
  67. * @param \Handlebars\Template $template The template instance 
  68. * @param \Handlebars\Context $context The current context 
  69. * @param array $args The arguments passed the the helper 
  70. * @param string $source The source 
  71. * @throws \InvalidArgumentException 
  72. * @return mixed The helper return value 
  73. */ 
  74. public function call($name, Template $template, Context $context, $args, $source) 
  75. if (!$this->has($name)) { 
  76. throw new \InvalidArgumentException('Unknown helper: ' . $name); 
  77.  
  78. if ($this->helpers[$name] instanceof Helper) { 
  79. return $this->helpers[$name]->execute( 
  80. $template, $context, $args, $source 
  81. ); 
  82.  
  83. return call_user_func($this->helpers[$name], $template, $context, $args, $source); 
  84.  
  85. /** 
  86. * Check if $name helper is available 
  87. * @param string $name helper name 
  88. * @return boolean 
  89. */ 
  90. public function has($name) 
  91. return array_key_exists($name, $this->helpers); 
  92.  
  93. /** 
  94. * Get a helper. __magic__ method :) 
  95. * @param string $name helper name 
  96. * @throws \InvalidArgumentException if $name is not available 
  97. * @return callable helper function 
  98. */ 
  99. public function __get($name) 
  100. if (!$this->has($name)) { 
  101. throw new \InvalidArgumentException('Unknown helper :' . $name); 
  102.  
  103. return $this->helpers[$name]; 
  104.  
  105. /** 
  106. * Check if $name helper is available __magic__ method :) 
  107. * @param string $name helper name 
  108. * @return boolean 
  109. * @see Handlebras_Helpers::has 
  110. */ 
  111. public function __isset($name) 
  112. return $this->has($name); 
  113.  
  114. /** 
  115. * Add a new helper to helpers __magic__ method :) 
  116. * @param string $name helper name 
  117. * @param callable $helper a function as a helper 
  118. * @return void 
  119. */ 
  120. public function __set($name, $helper) 
  121. $this->add($name, $helper); 
  122.  
  123. /** 
  124. * Unset a helper 
  125. * @param string $name helper name to remove 
  126. * @return void 
  127. */ 
  128. public function __unset($name) 
  129. $this->remove($name); 
  130.  
  131. /** 
  132. * Check whether a given helper is present in the collection. 
  133. * @param string $name helper name 
  134. * @throws \InvalidArgumentException if the requested helper is not present. 
  135. * @return void 
  136. */ 
  137. public function remove($name) 
  138. if (!$this->has($name)) { 
  139. throw new \InvalidArgumentException('Unknown helper: ' . $name); 
  140.  
  141. unset($this->helpers[$name]); 
  142.  
  143. /** 
  144. * Clear the helper collection. 
  145. * Removes all helpers from this collection 
  146. * @return void 
  147. */ 
  148. public function clear() 
  149. $this->helpers = array(); 
  150.  
  151. /** 
  152. * Check whether the helper collection is empty. 
  153. * @return boolean True if the collection is empty 
  154. */ 
  155. public function isEmpty() 
  156. return empty($this->helpers); 
  157.  
  158. /** 
  159. * Returns all helpers from the collection. 
  160. * @return array Associative array of helpers which keys are helpers names 
  161. * and the values are the helpers. 
  162. */ 
  163. public function getAll() 
  164. return $this->helpers;