HandlebarsHelperEachHelper

The Each Helper.

Defined (2)

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

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Helper/EachHelper.php  
  1. class EachHelper implements Helper 
  2. /** 
  3. * Execute the helper 
  4. * @param \Handlebars\Template $template The template instance 
  5. * @param \Handlebars\Context $context The current context 
  6. * @param array $args The arguments passed the the helper 
  7. * @param string $source The source 
  8. * @return mixed 
  9. */ 
  10. public function execute(Template $template, Context $context, $args, $source) 
  11. $tmp = $context->get($args); 
  12. $buffer = ''; 
  13.  
  14. if (!$tmp) { 
  15. $template->setStopToken('else'); 
  16. $template->discard(); 
  17. $template->setStopToken(false); 
  18. $buffer = $template->render($context); 
  19. } elseif (is_array($tmp) || $tmp instanceof \Traversable) { 
  20. $isList = is_array($tmp) && (array_keys($tmp) === range(0, count($tmp) - 1)); 
  21. $index = 0; 
  22. $lastIndex = $isList ? (count($tmp) - 1) : false; 
  23.  
  24. foreach ($tmp as $key => $var) { 
  25. $specialVariables = array( 
  26. '@index' => $index,  
  27. '@first' => ($index === 0),  
  28. '@last' => ($index === $lastIndex),  
  29. ); 
  30. if (!$isList) { 
  31. $specialVariables['@key'] = $key; 
  32. $context->pushSpecialVariables($specialVariables); 
  33. $context->push($var); 
  34. $template->setStopToken('else'); 
  35. $template->rewind(); 
  36. $buffer .= $template->render($context); 
  37. $context->pop(); 
  38. $context->popSpecialVariables(); 
  39. $index++; 
  40.  
  41. $template->setStopToken(false); 
  42.  
  43. return $buffer; 
/vendor/xamin/handlebars.php/src/Handlebars/Helper/EachHelper.php  
  1. class EachHelper implements Helper 
  2. /** 
  3. * Execute the helper 
  4. * @param \Handlebars\Template $template The template instance 
  5. * @param \Handlebars\Context $context The current context 
  6. * @param array $args The arguments passed the the helper 
  7. * @param string $source The source 
  8. * @return mixed 
  9. */ 
  10. public function execute(Template $template, Context $context, $args, $source) 
  11. $tmp = $context->get($args); 
  12. $buffer = ''; 
  13.  
  14. if (!$tmp) { 
  15. $template->setStopToken('else'); 
  16. $template->discard(); 
  17. $template->setStopToken(false); 
  18. $buffer = $template->render($context); 
  19. } elseif (is_array($tmp) || $tmp instanceof \Traversable) { 
  20. $isList = is_array($tmp) && (array_keys($tmp) === range(0, count($tmp) - 1)); 
  21. $index = 0; 
  22. $lastIndex = $isList ? (count($tmp) - 1) : false; 
  23.  
  24. foreach ($tmp as $key => $var) { 
  25. $specialVariables = array( 
  26. '@index' => $index,  
  27. '@first' => ($index === 0),  
  28. '@last' => ($index === $lastIndex),  
  29. ); 
  30. if (!$isList) { 
  31. $specialVariables['@key'] = $key; 
  32. $context->pushSpecialVariables($specialVariables); 
  33. $context->push($var); 
  34. $template->setStopToken('else'); 
  35. $template->rewind(); 
  36. $buffer .= $template->render($context); 
  37. $context->pop(); 
  38. $context->popSpecialVariables(); 
  39. $index++; 
  40.  
  41. $template->setStopToken(false); 
  42.  
  43. return $buffer;