/**
  * {@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;
 }
Beispiel #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;
 }
Beispiel #3
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;
 }
Beispiel #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)
 {
     $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
  *
  * @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 );
     $user = $context->get('user');
     if (!$user) {
         throw new \Exception("User need to be log-in to access to his addresses");
     }
     // Get Address
     // -----------------
     $options = ['where' => [['uid', '=', $user->uid]]];
     try {
         $addresses = Subbly::api('subbly.user_address')->findByUser($user, $options);
     } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
         return false;
     }
     if (!count($addresses)) {
         $addresses = false;
     }
     $buffer = '';
     if (!$addresses) {
         $template->setStopToken('else');
         $template->discard();
         $template->setStopToken(false);
         $buffer = $template->render($context);
     } elseif (is_array($addresses) || $addresses instanceof \Traversable) {
         $isList = is_array($addresses) && array_keys($addresses) === range(0, count($addresses) - 1);
         $index = 0;
         $lastIndex = $isList ? count($addresses) - 1 : false;
         foreach ($addresses 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;
 }
Beispiel #6
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)
 {
     $buffer = '';
     $cart = \Subbly\Subbly::api('subbly.cart')->count();
     if (!count($cart)) {
         $template->setStopToken('else');
         $template->discard($context);
         $template->setStopToken(false);
         $buffer = $template->render($context);
     } else {
         $template->setStopToken('else');
         $buffer = $template->render($context);
         $template->setStopToken(false);
         $template->discard($context);
     }
     return $buffer;
 }
Beispiel #7
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)
 {
     $parsedArgs = $template->parseArguments($args);
     $tmp = $context->get($parsedArgs[0]);
     $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;
 }
 /**
  * {@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;
 }
Beispiel #9
0
 /**
  * Execute the helper
  * {{#isUserLogin}}
  *
  * @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 = Subbly::api('subbly.user')->check();
     if (!$tmp) {
         $template->setStopToken('else');
         $buffer = $template->render($context);
         $template->setStopToken(false);
         $template->discard($context);
     } else {
         $context->push(['user' => Subbly::api('subbly.user')->currentUser()]);
         $template->setStopToken('else');
         $template->discard($context);
         $template->setStopToken(false);
         $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) != 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)
 {
     $arguments = $this->split($args);
     $left = $this->getData($context, $arguments[0]);
     $operator = $arguments[1]['data'];
     $right = $this->getData($context, $arguments[2]);
     if ($this->compare($left, $operator, $right)) {
         $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);
     }
     return $buffer;
 }
Beispiel #12
0
 /**
  * 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;
 }
 /**
  * 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);
     $dataSelector = implode(".", array_slice($args, 0, count($args) - 1));
     $tmp = $context->get($dataSelector);
     if (count($args) == 1) {
         $limit = 0;
     } else {
         $limit = $args[count($args) - 1];
     }
     $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) {
             if ($index + 1 > $limit && $limit > 0) {
                 continue;
             }
             $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)
 {
     // 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;
 }
Beispiel #15
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)
 {
     if (is_numeric($args)) {
         $tmp = $args;
     } else {
         $tmp = $context->get($args);
     }
     $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;
 }
Beispiel #16
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)
 {
     $props = $this->parseProps($args, $context);
     $args = $this->parseArgs($args);
     // Products query
     // ----------------
     $productsOptions = ['where' => [['status', '!=', 'draft'], ['status', '!=', 'hidden']]];
     // Includes
     // ----------------
     if (isset($props['includes']) && is_array($props['includes'])) {
         $productsOptions['includes'] = $props['includes'];
     }
     // Where
     // ----------------
     if (isset($props['where']) && is_array($props['where'])) {
         // $productsOptions['where'] = [];
         foreach ($props['where'] as $condition) {
             if (is_array($condition) && count($condition) == 3) {
                 $productsOptions['where'][] = $condition;
             }
         }
     }
     // Order By
     // ----------------
     if (isset($props['order_by']) && is_array($props['order_by'])) {
         $productsOptions['order_by'] = [];
         foreach ($props['order_by'] as $order) {
             if (is_array($order) && count($order) == 2) {
                 $productsOptions['order_by'][$order[0]] = $order[1];
             }
         }
     }
     // Offset & limit
     // ----------------
     if (isset($props['limit']) && is_integer($props['limit'])) {
         $productsOptions['limit'] = $props['limit'];
     }
     $offset = null;
     if (isset($props['offset']) && is_integer($props['offset']) && isset($productsOptions['limit'])) {
         $productsOptions['offset'] = $props['offset'];
     }
     if (!isset($productsOptions['offset']) && isset($props['page']) && is_integer($props['page'])) {
         $offset = ((int) $props['page'] - 1) * $productsOptions['limit'];
         $offset = $offset < 0 ? 0 : $offset;
     }
     $offset ?: 0;
     $productsOptions['offset'] = $offset;
     // Categories
     // ----------------
     $hasCategories = is_array($props) && (array_key_exists('category', $props) || array_key_exists('subcategory', $props));
     if ($hasCategories) {
         $categoriesOptions = [];
         if (isset($props['category'])) {
             $categoriesOptions[] = ['slug', $props['category']];
         }
         if (isset($props['subcategory'])) {
             $categoriesOptions[] = ['slug', $props['subcategory']];
         }
         if (count($categoriesOptions) > 0) {
             $categories = \Subbly\Subbly::api('subbly.category')->all(['has' => ['translations' => $categoriesOptions]]);
         } else {
             $hasCategories = false;
         }
     }
     if ($hasCategories && count($categories)) {
         $productsOptions['has']['categories'] = [];
         foreach ($categories as $category) {
             $productsOptions['has']['categories'][] = ['category_id', $category->id];
         }
     }
     // Get products
     // -----------------
     $products = \Subbly\Subbly::api('subbly.product')->all($productsOptions)->toArray();
     $buffer = '';
     if (!$products) {
         $template->setStopToken('else');
         $template->discard();
         $template->setStopToken(false);
         $buffer = $template->render($context);
     } elseif (is_array($products) || $products instanceof \Traversable) {
         $isList = is_array($products) && array_keys($products) === range(0, count($products) - 1);
         $index = 0;
         $lastIndex = $isList ? count($products) - 1 : false;
         foreach ($products 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;
 }