/**
  * {@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 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;
 }
Example #3
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;
 }
 /**
  * {@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;
 }
Example #5
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);
 }
 /**
  * {@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]));
 }
Example #7
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);
 }
Example #9
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;
 }
Example #10
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 || 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);
 }
Example #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.address');
     // edit recorded address
     // ------------------------
     $props = $this->parseProps($args, $context);
     $args = $template->parseArguments($args);
     $settings = $props ? array_merge($default, $props) : $default;
     $html = \Form::open($settings);
     $html .= \Form::hidden('addressId', $context->get('inputs.addressId'));
     return $html;
 }
 /**
  * {@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);
 }
Example #15
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) < 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;
 }
Example #16
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);
 }
Example #17
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) != 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);
 }
 /**
  * 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'];
         }
     }
 }
 /**
  * {@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);
 }
Example #21
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('"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;
 }
Example #24
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)
 {
     $parsedArgs = $template->parseArguments($args);
     $tmp = $context->get($parsedArgs[0]);
     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('"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));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function execute(Template $template, Context $context, $args, $source)
 {
     $resolved_args = array();
     foreach ($template->parseArguments($args) as $arg) {
         $resolved_args[] = $context->get($arg);
     }
     if ($this->evaluateCondition($resolved_args)) {
         $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;
 }
Example #27
0
 /**
  * Execute the helper
  * {{price 12.00}}
  * {{price 12.00 'USD'}}
  * {{price this 'USD'}}
  * {{price this}}
  *
  * @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)
 {
     $currencies = \Config::get('currency');
     $buffer = '';
     $args = $template->parseArguments($args);
     $countAgrs = count($args);
     $symbol_style = '%symbol%';
     if ($countAgrs == 0) {
         return $buffer;
     }
     if ($args[0] == 'this') {
         $product = $context->get('this');
         $value = $product['price'];
     } else {
         if (is_numeric($args[0])) {
             $value = $args[0];
         }
     }
     if ($countAgrs === 1) {
         $currency = $currencies['table'][$currencies['default']];
     } else {
         $currency = isset($currencies['table'][$args[1]->getString()]) ? $currencies['table'][$args[1]->getString()] : $currencies['table'][$currencies['default']];
     }
     $symbol_left = $currency['symbol_left'];
     $symbol_right = $currency['symbol_right'];
     $decimal_place = $currency['decimal_place'];
     $decimal_point = $currency['decimal_point'];
     $thousand_point = $currency['thousand_point'];
     $string = '';
     if ($symbol_left) {
         $string .= str_replace('%symbol%', $symbol_left, $symbol_style);
         if ($currencies['use_space']) {
             $string .= ' ';
         }
     }
     $string .= number_format(round($value, (int) $decimal_place), (int) $decimal_place, $decimal_point, $thousand_point);
     if ($symbol_right) {
         if ($currencies['use_space']) {
             $string .= ' ';
         }
         $string .= str_replace('%symbol%', $symbol_right, $symbol_style);
     }
     return $string;
 }
 /**
  * {@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;
 }
 /**
  * {@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('"ifOverridden" helper expects exactly one argument.');
     }
     $block_name = $context->get(array_shift($parsed_args));
     // Check condition and render blocks
     if ($this->blocksStorage->has($block_name)) {
         $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)
 {
     $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;
     }
 }