Ejemplo n.º 1
0
 /**
  * Process section nodes
  *
  * @param Handlebars_Context $context current context
  * @param array   $current section node data
  *
  * @throws {$1}
  * @return {string} the result
  */
 private function _section(Handlebars_Context $context, $current)
 {
     $helpers = $this->handlebars->getHelpers();
     $sectionName = $current[Handlebars_Tokenizer::NAME];
     if ($helpers->has($sectionName)) {
         return $this->_handlebarsStyleSection($context, $current);
     } elseif (trim($current[Handlebars_Tokenizer::ARGS]) == '') {
         return $this->_mustacheStyleSection($context, $current);
     } else {
         throw new RuntimeException($sectionName . ' is not registered as a helper');
     }
 }
Ejemplo n.º 2
0
 /**
  * Process section nodes
  *
  * @param Context $context current context
  * @param array   $current section node data
  *
  * @throws \RuntimeException
  * @return string the result
  */
 private function _section(Context $context, $current)
 {
     $helpers = $this->handlebars->getHelpers();
     $sectionName = $current[Tokenizer::NAME];
     if ($helpers->has($sectionName)) {
         if (isset($current[Tokenizer::END])) {
             $source = substr($this->getSource(), $current[Tokenizer::INDEX], $current[Tokenizer::END] - $current[Tokenizer::INDEX]);
         } else {
             $source = '';
         }
         $params = array($this, $context, $current[Tokenizer::ARGS], $source);
         $return = call_user_func_array($helpers->{$sectionName}, $params);
         if ($return instanceof String) {
             return $this->handlebars->loadString($return)->render($context);
         } else {
             return $return;
         }
     } elseif (trim($current[Tokenizer::ARGS]) == '') {
         // fallback to mustache style each/with/for just if there is
         // no argument at all.
         try {
             $sectionVar = $context->get($sectionName, true);
         } catch (\InvalidArgumentException $e) {
             throw new \RuntimeException($sectionName . ' is not registered as a helper');
         }
         $buffer = '';
         if (is_array($sectionVar) || $sectionVar instanceof \Traversable) {
             foreach ($sectionVar as $index => $d) {
                 $context->pushIndex($index);
                 $context->push($d);
                 $buffer .= $this->render($context);
                 $context->pop();
                 $context->popIndex();
             }
         } elseif (is_object($sectionVar)) {
             //Act like with
             $context->push($sectionVar);
             $buffer = $this->render($context);
             $context->pop();
         } elseif ($sectionVar) {
             $buffer = $this->render($context);
         }
         return $buffer;
     } else {
         throw new \RuntimeException($sectionName . ' is not registered as a helper');
     }
 }