/** * Execute the sanitize Helper for Handlebars.php {{sanitize field callback}} * * @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) { $postionalArgs = $template->parseArguments($args); if (isset($postionalArgs[1])) { $func = $postionalArgs[1]; $arg = $context->get($postionalArgs[0]); } else { $func = $postionalArgs[0]; } if (is_object($func)) { $func = $context->get($func); } else { $func = $func; } if (!empty($func) && function_exists($func)) { ob_start(); if (isset($arg)) { $output = $func($arg); } else { $output = $func(); } $has_output = ob_get_clean(); if (!empty($has_output) && !is_bool($has_output)) { return $has_output; } return $output; } return $arg; }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { // Check if there is at least one argument $parsed_arguments = $template->parseArguments($args); if (count($parsed_arguments) == 0) { throw new \InvalidArgumentException('"l10n" helper expects at least one argument.'); } $text = $context->get(array_shift($parsed_arguments)); // We need to escape extra arguments passed to the helper. Thus we need // to get escape function and its arguments from the template engine. $escape_func = $template->getEngine()->getEscape(); $escape_args = $template->getEngine()->getEscapeArgs(); // Check if there are any other arguments passed into helper and escape // them. $local_args = array(); foreach ($parsed_arguments as $parsed_argument) { // Get locale argument string and add it to escape function // arguments. array_unshift($escape_args, $context->get($parsed_argument)); // Escape locale argument's value $local_args[] = call_user_func_array($escape_func, array_values($escape_args)); // Remove locale argument's value from escape function argument array_shift($escape_args); } $result = getlocal($text, $local_args); return new SafeString($result); }
/** * Execute the helper * {{default varable 'default string'}} * * @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) { $args = $template->parseArguments($args); if (count($args) < 2) { throw new \Exception("Handlerbars Helper 'default' needs 2 parameters"); } $asked = $context->get($args[0]); $default = $context->get($args[1]); return $asked ? $asked : $default; }
/** * {@inheritdoc} * * @todo Use combined arguments parser when it will be implemented in * Handlebars.php. */ public function execute(Template $template, Context $context, $args, $source) { $named_args = $template->parseNamedArguments($args); $positional_args = $template->parseArguments($args); $route_name = (string) $context->get($positional_args[0]); $parameters = array(); foreach ($named_args as $name => $parsed_arg) { $parameters[$name] = $context->get($parsed_arg); } return $this->getRouter()->generate($route_name, $parameters); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) < 1 || count($parsed_args) > 2) { throw new \InvalidArgumentException('"generatePagination" helper expects one or two arguments.'); } $pagination_info = $context->get($parsed_args[0]); $bottom = empty($parsed_args[1]) ? true : $context->get($parsed_args[1]); $pagination = generate_pagination($pagination_info, $bottom === "false" ? false : true); return new SafeString($pagination); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 2) { throw new \InvalidArgumentException('"replace" helper expects exactly two arguments.'); } $search = $context->get($parsed_args[0]); $replacement = $context->get($parsed_args[1]); $subject = (string) $template->render($context); return str_replace($search, $replacement, $subject); }
/** * 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 * {{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) != 1) { throw new \InvalidArgumentException('"assets" helper expects exactly one argument.'); } // Prevent `extends` sub level if (is_null($themePath = $context->get('themes'))) { $themePath = $context->get('../themes'); } return $themePath . DS . $args[0]->getString(); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 2) { throw new \InvalidArgumentException('"formatDate" helper expects exactly two arguments.'); } $raw_time = $context->get($parsed_args[0]); if ($raw_time instanceof \DateTime) { $timestamp = $raw_time->getTimestamp(); } else { $timestamp = intval($raw_time); } $format = $context->get($parsed_args[1]); return strftime($format, $timestamp); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) == 0) { throw new \InvalidArgumentException('"ifAny" helper expects at least one argument.'); } $condition = false; foreach ($parsed_args as $parsed_arg) { $value = $context->get($parsed_arg); if ($value instanceof String) { // Casting any object of \Handlebars\Str will have false // positive result even for those with empty internal strings. // Thus we need to check internal string of such objects. $value = $value->getString(); } if ($value) { $condition = true; break; } } if ($condition) { $template->setStopToken('else'); $buffer = $template->render($context); $template->setStopToken(false); } else { $template->setStopToken('else'); $template->discard(); $template->setStopToken(false); $buffer = $template->render($context); } 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); $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; }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 1) { throw new \InvalidArgumentException('"last" helper expects exactly one argument.'); } $collection = $context->get($parsed_args[0]); if (!is_array($collection) && !$collection instanceof \Traversable) { throw new \InvalidArgumentException('Wrong type of the argument in the "last" helper.'); } if (is_array($collection)) { return end($collection); } // "end" function does not work with \Traversable in HHVM. Thus we // need to get the element manually. while ($collection instanceof \IteratorAggregate) { $collection = $collection->getIterator(); } $collection->rewind(); $item = false; while ($collection->valid()) { $item = $collection->current(); $collection->next(); } return $item; }
/** * Execute the vardump Helper for Handlebars.php {{_vardump vardump_id size}} * * @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) { ob_start(); var_dump($context->get($args)); $output = ob_get_clean(); return new \Handlebars\SafeString('<pre>' . $output . '</pre>'); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 1) { throw new \InvalidArgumentException('"uppercase" helper expects exactly one argument.'); } return strtoupper($context->get($parsed_args[0])); }
/** * Execute the _image Helper for Handlebars.php {{_image image_id size}} * 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) { if (strlen($args) <= 0) { return $context->get('image'); } $parts = explode(" ", $args); $tmp = $context->get(trim($parts[0])); if (isset($tmp['ID'])) { $url = wp_get_attachment_image_src($tmp['ID']); } else { $url = wp_get_attachment_image_src($tmp, !empty($parts[1]) ? trim($parts[1]) : 'thumbnail'); } if (is_array($url) && isset($url[0])) { return $url[0]; } return null; }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 1) { throw new \InvalidArgumentException('"formatDateDiff" helper expects exactly one argument.'); } $seconds = intval($context->get($parsed_args[0])); return date_diff_to_text($seconds); }
/** * Execute the helper * {{capitalize 'some word'}} * {{capitalize name}} * * @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) != 1) { return $buffer; } return ucwords(mb_strtolower($context->get($args[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(Template $template, Context $context, $args, $source) { $default = array('route' => 'subbly.postcontroller.updatecart'); $cart = $context->get('this'); if (!isset($cart['rowid'])) { throw new FrontageInvalidHelperException('"carUpdateQty" helper need to be inside the "cart" helper.'); } return $this->buildForm($template, $context, $args, $default, ['cartRowId' => $cart['rowid']]); }
/** * 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) { $default = array('route' => 'subbly.postcontroller.addtocart'); $product = $context->get('this'); if (!isset($product['id']) && !isset($product['id'])) { throw new FrontageInvalidHelperException('"AddToCar" helper need to be inside a "product".'); } return $this->buildForm($template, $context, $args, $default, ['id' => $product['id']]); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 2) { throw new \InvalidArgumentException('"ifEqual" helper expects exactly two arguments.'); } $condition = $context->get($parsed_args[0]) == $context->get($parsed_args[1]); if ($condition) { $template->setStopToken('else'); $buffer = $template->render($context); $template->setStopToken(false); } else { $template->setStopToken('else'); $template->discard(); $template->setStopToken(false); $buffer = $template->render($context); } 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) { $args = $this->ParseArgs($args); $tmp = $context->get($args[0]); ob_start(); var_dump($tmp); $vd = ob_get_contents(); ob_end_clean(); return $vd; }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 1) { throw new \InvalidArgumentException('"last" helper expects exactly one argument.'); } $collection = $context->get($parsed_args[0]); if (!is_array($collection) && !$collection instanceof \Countable) { throw new \InvalidArgumentException('Wrong type of the argument in the "count" helper.'); } return count($collection); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) < 2 || count($parsed_args) > 3) { throw new \InvalidArgumentException('"truncate" helper expects two or three arguments.'); } $string = (string) $context->get($parsed_args[0]); $length = intval($context->get($parsed_args[1])); $append = isset($parsed_args[2]) ? (string) $context->get($parsed_args[2]) : ''; if ($length < 0) { throw new \InvalidArgumentException('The second argument of "truncate" helper has to be greater than or equal to 0.'); } if ($append && strlen($append) > $length) { throw new \InvalidArgumentException('Cannot truncate string. Length of append value is greater than target length.'); } if (strlen($string) > $length) { return substr($string, 0, $length - strlen($append)) . $append; } else { return $string; } }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 1) { throw new \InvalidArgumentException('"repeat" helper expects exactly one argument.'); } $times = intval($context->get($parsed_args[0])); if ($times < 0) { throw new \InvalidArgumentException('The first argument of "repeat" helper has to be greater than or equal to 0.'); } $string = $template->render($context); return str_repeat($string, $times); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 1) { throw new \InvalidArgumentException('"asset" helper expects exactly one argument.'); } $relative_path = $context->get($parsed_args[0]); if (preg_match("/^@(\\w+)\\//", $relative_path, $matches)) { // Resolve locations $relative_path = substr_replace($relative_path, $this->resolveLocation($matches[1]), 0, strlen($matches[0]) - 1); } return $this->getAssetUrlGenerator()->generate($relative_path); }
/** * 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); 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); } 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); $countAgrs = count($args); if ($countAgrs != 1) { return $buffer; } if ($args[0] == 'this') { $product = $context->get('this'); if (isset($product['images']) && count($product['images']) > 0) { return $product['images'][0]['filename']; } } }
/** * Execute the helper * {{input 'fieldname'}} * {{input 'fieldname' default}} * * @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) { $args = $template->parseArguments($args); $countAgrs = count($args); if ($countAgrs == 0) { return ''; } if (!$args[0] instanceof \Handlebars\String) { return ''; } $old = \Input::old($args[0]->getString(), false); if ($old) { return $old; } return \Input::get($args[0]->getString(), $context->get($args[0]->getString())); }
/** * {@inheritdoc} */ public function execute(Template $template, Context $context, $args, $source) { // Get block name $parsed_args = $template->parseArguments($args); if (count($parsed_args) != 1) { throw new \InvalidArgumentException('"override" helper expects exactly one argument.'); } $block_name = $context->get(array_shift($parsed_args)); // We need to provide unlimited inheritence level. Rendering is started // from the deepest level template. If the content is in the block // storage it is related with the deepest level template. Thus we do not // need to override it. if (!$this->blocksStorage->has($block_name)) { $this->blocksStorage->set($block_name, $template->render($context)); } }
/** * Execute the callback Helper for Handlebars.php {{callback callback_function [args,]}} * * @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) { $postionalArgs = $template->parseArguments($args); $function = array_shift($postionalArgs); $args = array(); foreach ((array) $postionalArgs as $arg) { $args[] = $context->get($arg); } ob_start(); $output = call_user_func_array($function, $args); $has_output = ob_get_clean(); if (!empty($has_output) && !is_bool($has_output)) { return $has_output; } return $output; }