Example #1
0
 /**
  * Execute the helper
  *
  * @param Handlebars_Template $template The template instance
  * @param Handlebars_Context  $context  The current context
  * @param array                $args     The arguments passed the the helper
  * @param string               $source   The source
  *
  * @return {mixed}
  */
 public function execute(Handlebars_Template $template, Handlebars_Context $context, $args, $source)
 {
     $tmp = $context->get($args);
     $buffer = '';
     if (!$tmp) {
         $template->setStopToken('else');
         $template->discard();
         $template->setStopToken(false);
         $buffer = $template->render($context);
     } elseif (is_array($tmp) || $tmp instanceof Traversable) {
         $isList = is_array($tmp) && array_keys($tmp) === range(0, count($tmp) - 1);
         $index = 0;
         $lastIndex = $isList ? count($tmp) - 1 : false;
         foreach ($tmp as $key => $var) {
             $specialVariables = array('@index' => $index, '@first' => $index === 0, '@last' => $index === $lastIndex);
             if (!$isList) {
                 $specialVariables['@key'] = $key;
             }
             $context->pushSpecialVariables($specialVariables);
             $context->push($var);
             $template->setStopToken('else');
             $template->rewind();
             $buffer .= $template->render($context);
             $context->pop();
             $context->popSpecialVariables();
             $index++;
         }
         $template->setStopToken(false);
     }
     return $buffer;
 }
Example #2
0
 /**
  * Execute the helper
  *
  * @param Handlebars_Template $template The template instance
  * @param Handlebars_Context  $context  The current context
  * @param array                $args     The arguments passed the the helper
  * @param string               $source   The source
  *
  * @return {mixed}
  */
 public function execute(Handlebars_Template $template, Handlebars_Context $context, $args, $source)
 {
     $parsedArgs = $template->parseArguments($args);
     $tmp = $context->get($parsedArgs[0]);
     $context->push($context->last());
     if ($tmp) {
         $template->setStopToken('else');
         $buffer = $template->render($context);
         $template->setStopToken(false);
         $template->discard($context);
     } else {
         $template->setStopToken('else');
         $template->discard($context);
         $template->setStopToken(false);
         $buffer = $template->render($context);
     }
     $context->pop();
     return $buffer;
 }