/**
  * {@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;
 }
Exemple #2
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)
 {
     $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) == 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;
 }
Exemple #4
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)
 {
     $context->with($args);
     $buffer = $template->render($context);
     $context->pop();
     return $buffer;
 }
 /**
  * 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;
 }
Exemple #6
0
 /**
  * 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 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>');
 }
Exemple #8
0
 /**
  * {@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);
 }
Exemple #9
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;
 }
 /**
  * {@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 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])));
 }
 /**
  * {@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);
 }
Exemple #13
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.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']]);
 }
 /**
  * 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']]);
 }
Exemple #15
0
 /**
  * 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;
 }
 /**
  * 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 || 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);
 }
Exemple #18
0
 /**
  * {@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) != 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);
 }
 /**
  * {@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);
 }
Exemple #21
0
 /**
  * {@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);
 }
 /**
  * {@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);
 }
Exemple #23
0
 /**
  * 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);
 }
 /**
  * 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'];
         }
     }
 }
Exemple #26
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)
 {
     $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;
 }
 /**
  * {@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('"block" helper expects exactly one argument.');
     }
     $block_name = $context->get(array_shift($parsed_args));
     // If the block is not overridden render and show the default value
     if (!$this->blocksStorage->has($block_name)) {
         return $template->render($context);
     }
     $content = $this->blocksStorage->get($block_name);
     // Show overridden content
     return $content;
 }
 /**
  * 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;
 }
Exemple #29
0
 /**
  * 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));
     }
 }