/** * Execute the helper * {{#formErrors login}} * <ul> * {{#each errors}} * <li>{{this}}</li> * {{/each}} * </ul> * {{/formErrors}} * Catch form validation's erros. * * @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(Template $template, Context $context, $args, $source) { $buffer = ''; $args = $template->parseArguments($args); $errors = \Session::get('errors', new \Illuminate\Support\MessageBag()); // no form specified // return empty buffer if (!count($args)) { return $buffer; } // if MessageBag does not exists // return empty buffer if (!method_exists($errors, 'hasBag')) { return $buffer; } // Defined MessageBag exists // so we push errors list to the context if ($errors->hasBag($args[0])) { $context->push(['errors' => $errors->{$args[0]}->all()]); $template->rewind(); $buffer .= $template->render($context); $context->pop(); return $buffer; } // return empty buffer return $buffer; }
/** * Execute the helper * {{url 'filename'}} * {{url 'filename' this }} * {{url 'filename' with {"id":12, "slug":"test"} }} * * @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(Template $template, Context $context, $args, $source) { $buffer = ''; $args = $template->parseArguments($args); if (count($args) < 2) { throw new \Exception("Handlerbars Helper 'compare' needs 2 parameters"); } $operator = isset($args[2]) ? $args[2]->getString() : '=='; $lvalue = $context->get($args[0]); $rvalue = $context->get($args[1]); $equal = function ($l, $r) { return $l == $r; }; $strictequal = function ($l, $r) { return $l === $r; }; $different = function ($l, $r) { return $l != $r; }; $lower = function ($l, $r) { return $l < $r; }; $greatter = function ($l, $r) { return $l > $r; }; $lowOrEq = function ($l, $r) { return $l <= $r; }; $greatOrEq = function ($l, $r) { return $l >= $r; }; $operators = ['==' => 'equal', '===' => 'strictequal', '!=' => 'different', '<' => 'lower', '>' => 'greatter', '<=' => 'lowOrEq', '>=' => 'greatOrEq']; if (!$operators[$operator]) { throw new \Exception("Handlerbars Helper 'compare' doesn't know the operator " . $operator); } $tmp = ${$operators}[$operator]($lvalue, $rvalue); $buffer = ''; $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; }
/** * 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(Template $template, Context $context, $args, $source) { $tmp = $context->get($args); $context->push($context->last()); if (!$tmp) { $template->setStopToken('else'); $buffer = $template->render($context); $template->setStopToken(false); } else { $template->setStopToken('else'); $template->discard(); $template->setStopToken(false); $buffer = $template->render($context); } $context->pop(); return $buffer; }
/** * Execute the is Helper for Handlebars.php {{#is variable value}} code {{else}} alt code {{/is}} * OR {{#is user_logged_in}} code {{else}} alt code {{/is}} * OR {{#is is_user_logged_in}} code {{else}} alt code {{/is}} * OR {{#is home}} code {{else}} alt code {{/is}} * based off the IfHelper * * @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 static function helper($template, $context, $args, $source) { $value = null; if (false !== strpos($args, ' ')) { $parts = explode(' ', $args); $args = $parts[0]; $value = $parts[1]; } else { if (in_array($args, self::if_checks())) { // fix alias - because is is_user is annouting..! if (false === strpos($args, 'is_')) { $args = 'is_' . $args; } if (call_user_func($args)) { $value = 1; $args = 1; } else { $value = null; $args = 1; } } } if (is_numeric($args)) { $tmp = $args; } else { $tmp = $context->get($args); } $context->push($context->last()); if ($tmp === $value) { $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; }
/** * Process Mustache section style * * @param Context $context current context * @param array $current section node data * * @throws \RuntimeException * @return mixed|string */ private function _mustacheStyleSection(Context $context, $current) { $sectionName = $current[Tokenizer::NAME]; // fallback to mustache style each/with/for just if there is // no argument at all. try { $sectionVar = $context->get($sectionName, false); } catch (\InvalidArgumentException $e) { throw new \RuntimeException(sprintf('"%s" is not registered as a helper', $sectionName)); } $buffer = ''; if (is_array($sectionVar) || $sectionVar instanceof \Traversable) { $isList = is_array($sectionVar) && array_keys($sectionVar) === range(0, count($sectionVar) - 1); $index = 0; $lastIndex = $isList ? count($sectionVar) - 1 : false; foreach ($sectionVar as $key => $d) { $specialVariables = array('@index' => $index, '@first' => $index === 0, '@last' => $index === $lastIndex); if (!$isList) { $specialVariables['@key'] = $key; } $context->pushSpecialVariables($specialVariables); $context->push($d); $buffer .= $this->render($context); $context->pop(); $context->popSpecialVariables(); $index++; } } elseif (is_object($sectionVar)) { //Act like with $context->push($sectionVar); $buffer = $this->render($context); $context->pop(); } elseif ($sectionVar) { $buffer = $this->render($context); } return $buffer; }