/** * 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; }