예제 #1
0
 /**
  * Execute the helper
  *
  * @param \Handlebars\Template  $template The template instance
  * @param \Handlebars\Context   $context  The current context
  * @param \Handlebars\Arguments $args     The arguments passed the the helper
  * @param string                $source   The source
  *
  * @return mixed
  */
 public function execute(Template $template, Context $context, $args, $source)
 {
     $positionalArgs = $args->getPositionalArguments();
     $tmp = $context->get($positionalArgs[0]);
     $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;
 }
예제 #2
0
 /**
  * Execute the helper
  *
  * @param \Handlebars\Template  $template The template instance
  * @param \Handlebars\Context   $context  The current context
  * @param \Handlebars\Arguments $args     The arguments passed the the helper
  * @param string                $source   The source
  *
  * @return mixed
  */
 public function execute(Template $template, Context $context, $args, $source)
 {
     $positionalArgs = $args->getPositionalArguments();
     $context->with($positionalArgs[0]);
     $buffer = $template->render($context);
     $context->pop();
     return $buffer;
 }
예제 #3
0
 /**
  * Break an argument string into an array of strings
  *
  * @param string $string Argument String as passed to a helper
  *
  * @throws \RuntimeException
  * @return array the argument list as an array
  */
 public function parseArguments($string)
 {
     if ($string instanceof Arguments) {
         // This code is needed only for backward compatibility
         $args = $string;
     } else {
         $args = new Arguments($string);
     }
     return $args->getPositionalArguments();
 }