/**
  * Renders the current node
  *
  * @param LiquidContext $context
  * @return string
  */
 public function render(&$context)
 {
     $collection = $context->get($this->collection_name);
     if (!is_array($collection)) {
         die(debug('not array', $collection));
     }
     // discard keys
     $collection = array_values($collection);
     if (isset($this->attributes['limit']) || isset($this->attributes['offset'])) {
         $limit = $context->get($this->attributes['limit']);
         $offset = $context->get($this->attributes['offset']);
         $collection = array_slice($collection, $offset, $limit);
     }
     $length = count($collection);
     $cols = $context->get($this->attributes['cols']);
     $row = 1;
     $col = 0;
     $result = "<tr class=\"row1\">\n";
     $context->push();
     foreach ($collection as $index => $item) {
         $context->set($this->variable_name, $item);
         $context->set('tablerowloop', array('length' => $length, 'index' => $index + 1, 'index0' => $index, 'rindex' => $length - $index, 'rindex0' => $length - $index - 1, 'first' => (int) ($index == 0), 'last' => (int) ($index == $length - 1)));
         $result .= "<td class=\"col" . ++$col . "\">" . $this->render_all($this->_nodelist, $context) . "</td>";
         if ($col == $cols && !($index == $length - 1)) {
             $col = 0;
             $result .= "</tr>\n<tr class=\"row" . ++$row . "\">";
         }
     }
     $context->pop();
     $result .= "</tr>\n";
     return $result;
 }
 /**
  * Render the tag
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     $context->push();
     $logicalRegex = new LiquidRegexp('/\\s+(and|or)\\s+/');
     $conditionalRegex = new LiquidRegexp('/(' . LIQUID_QUOTED_FRAGMENT . ')\\s*([=!<>a-z_]+)?\\s*(' . LIQUID_QUOTED_FRAGMENT . ')?/');
     $result = '';
     foreach ($this->_blocks as $i => $block) {
         if ($block[0] == 'else') {
             $result = $this->render_all($block[2], $context);
             break;
         }
         if ($block[0] == 'if' || $block[0] == 'elsif') {
             /* Extract logical operators */
             $logicalRegex->match($block[1]);
             $logicalOperators = $logicalRegex->matches;
             array_shift($logicalOperators);
             /* Extract individual conditions */
             $temp = $logicalRegex->split($block[1]);
             $conditions = array();
             foreach ($temp as $condition) {
                 if ($conditionalRegex->match($condition)) {
                     $left = isset($conditionalRegex->matches[1]) ? $conditionalRegex->matches[1] : null;
                     $operator = isset($conditionalRegex->matches[2]) ? $conditionalRegex->matches[2] : null;
                     $right = isset($conditionalRegex->matches[3]) ? $conditionalRegex->matches[3] : null;
                     array_push($conditions, array('left' => $left, 'operator' => $operator, 'right' => $right));
                 } else {
                     throw new LiquidException("Syntax Error in tag 'if' - Valid syntax: if [condition]");
                 }
             }
             if (count($logicalOperators)) {
                 /* If statement contains and/or */
                 $display = true;
                 foreach ($logicalOperators as $k => $logicalOperator) {
                     if ($logicalOperator == 'and') {
                         $display = $this->interpret_condition($conditions[$k]['left'], $conditions[$k]['right'], $conditions[$k]['operator'], $context) && $this->interpret_condition($conditions[$k + 1]['left'], $conditions[$k + 1]['right'], $conditions[$k + 1]['operator'], $context);
                     } else {
                         $display = $this->interpret_condition($conditions[$k]['left'], $conditions[$k]['right'], $conditions[$k]['operator'], $context) || $this->interpret_condition($conditions[$k + 1]['left'], $conditions[$k + 1]['right'], $conditions[$k + 1]['operator'], $context);
                     }
                 }
             } else {
                 /* If statement is a single condition */
                 $display = $this->interpret_condition($conditions[0]['left'], $conditions[0]['right'], $conditions[0]['operator'], $context);
             }
             if ($display) {
                 $result = $this->render_all($block[2], $context);
                 break;
             }
         }
     }
     $context->pop();
     return $result;
 }
 /**
  * Renders the tag
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     // if the value is not set in the environment check to see if it
     // exists in the context, and if not set it to 0
     if (!isset($context->environments[0][$this->_toDecrement])) {
         // check for a context value
         $from_context = $context->get($this->_toDecrement);
         // we already have a value in the context
         $context->environments[0][$this->_toDecrement] = null !== $from_context ? $from_context : 0;
     }
     // decrement the environment value
     $context->environments[0][$this->_toDecrement]--;
 }
 /**
  * Renders the tag
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     $output = $context->get($this->_from);
     foreach ($this->filters as $filter) {
         list($filtername, $filter_arg_keys) = $filter;
         $filter_arg_values = array();
         foreach ($filter_arg_keys as $arg_key) {
             $filter_arg_values[] = $context->get($arg_key);
         }
         $output = $context->invoke($filtername, $output, $filter_arg_values);
     }
     $context->set($this->_to, $output);
 }
 /**
  * Renders the variable with the data in the context
  *
  * @param LiquidContext $context
  */
 function render($context)
 {
     $output = $context->get($this->_name);
     foreach ($this->_filters as $filter) {
         list($filtername, $filterArgKeys) = $filter;
         $filterArgValues = array();
         foreach ($filterArgKeys as $arg_key) {
             $filterArgValues[] = $context->get($arg_key);
         }
         $output = $context->invoke($filtername, $output, $filterArgValues);
     }
     return $output;
 }
 /**
  * Renders the variable with the data in the context
  *
  * @param LiquidContext $context
  */
 function render($context)
 {
     $output = $context->get($this->_name);
     //debug('name', $this->_name, 'output', $output);
     foreach ($this->filters as $filter) {
         list($filtername, $filter_arg_keys) = $filter;
         $filter_arg_values = array();
         foreach ($filter_arg_keys as $arg_key) {
             $filter_arg_values[] = $context->get($arg_key);
         }
         $output = $context->invoke($filtername, $output, $filter_arg_values);
     }
     return $output;
 }
Example #7
0
 function test_cents_through_drop_nestedly()
 {
     $this->context->merge(array('cents' => array('cents' => new CentsDrop())));
     $this->assertEqual(100, $this->context->get('cents.cents.amount'));
     $this->context->merge(array('cents' => array('cents' => array('cents' => new CentsDrop()))));
     $this->assertEqual(100, $this->context->get('cents.cents.cents.amount'));
 }
 /**
  * Interpret a comparison 
  *
  * @param string $left
  * @param string $right
  * @param string $op
  * @param LiquidContext $context
  * @return bool
  */
 protected function _interpretCondition($left, $right, $op = null, &$context)
 {
     if (is_null($op)) {
         $value = $this->_stringValue($context->get($left));
         return $value;
     }
     // values of 'empty' have a special meaning in array comparisons
     if ($right == 'empty' && is_array($context->get($left))) {
         $left = count($context->get($left));
         $right = 0;
     } elseif ($left == 'empty' && is_array($context->get($right))) {
         $right = count($context->get($right));
         $left = 0;
     } else {
         $left = $context->get($left);
         $right = $context->get($right);
         $left = $this->_stringValue($left);
         $right = $this->_stringValue($right);
     }
     // special rules for null values
     if (is_null($left) || is_null($right)) {
         // null == null returns true
         if ($op == '==' && is_null($left) && is_null($right)) {
             return true;
         }
         // null != anything other than null return true
         if ($op == '!=' && (!is_null($left) || !is_null($right))) {
             return true;
         }
         // everything else, return false;
         return false;
     }
     // regular rules
     switch ($op) {
         case '==':
             return $left == $right;
         case '!=':
             return $left != $right;
         case '>':
             return $left > $right;
         case '<':
             return $left < $right;
         case '>=':
             return $left >= $right;
         case '<=':
             return $left <= $right;
         case 'contains':
             return is_array($left) ? in_array($right, $left) : $left == $right || strpos($left, $right);
         default:
             throw new LiquidException("Error in tag '" . $this->name() . "' - Unknown operator {$op}");
     }
 }
 /**
  * Renders the tag
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     if (!isset($context->registers['for'])) {
         $context->registers['for'] = array();
     }
     $collection = $context->get($this->_collectionName);
     if (is_null($collection) || !is_array($collection) || count($collection) == 0) {
         return '';
     }
     $range = array(0, count($collection));
     if (isset($this->_attributes['limit']) || isset($this->_attributes['offset'])) {
         $offset = 0;
         if (isset($this->_attributes['offset'])) {
             $offset = $this->_attributes['offset'] == 'continue' ? $context->registers['for'][$this->_name] : $context->get($this->_attributes['offset']);
         }
         //$limit = $context->get($this->_attributes['limit']);
         $limit = isset($this->_attributes['limit']) ? $context->get($this->_attributes['limit']) : null;
         $range_end = $limit ? $limit : count($collection) - $offset;
         $range = array($offset, $range_end);
         $context->registers['for'][$this->_name] = $range_end + $offset;
     }
     $result = '';
     $segment = array_slice($collection, $range[0], $range[1]);
     if (!count($segment)) {
         return null;
     }
     $context->push();
     $length = count($segment);
     /**
      * @todo If $segment keys are not integer, forloop not work
      * array_values is only a little help without being tested.
      */
     $segment = array_values($segment);
     foreach ($segment as $index => $item) {
         $context->set($this->_variableName, $item);
         $context->set('forloop', array('name' => $this->_name, 'length' => $length, 'index' => $index + 1, 'index0' => $index, 'rindex' => $length - $index, 'rindex0' => $length - $index - 1, 'first' => (int) ($index == 0), 'last' => (int) ($index == $length - 1)));
         $result .= $this->renderAll($this->_nodelist, $context);
     }
     $context->pop();
     return $result;
 }
 /**
  * Renders the tag
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     $this->_collection = $context->get($this->_collectionName);
     $this->_collectionSize = count($this->_collection);
     $this->_totalPages = ceil($this->_collectionSize / $this->_numberItems);
     $paginated_collection = array_slice($this->_collection, $this->_currentOffset, $this->_numberItems);
     // Sets the collection if it's a key of another collection (ie search.results, collection.products, blog.articles)
     $segments = explode('.', $this->_collectionName);
     if (count($segments) == 2) {
         $context->set($segments[0], array($segments[1] => $paginated_collection));
     } else {
         $context->set($this->_collectionName, $paginated_collection);
     }
     $paginate = array('page_size' => $this->_numberItems, 'current_page' => $this->_currentPage, 'current_offset' => $this->_currentOffset, 'pages' => $this->_totalPages, 'items' => $this->_collectionSize, 'previous' => false, 'next' => false);
     if ($this->_currentPage != 1) {
         $paginate['previous'] = array('title' => '&laquo; Previous', 'url' => $this->current_url() . '?page=' . ($this->_currentPage - 1));
     }
     if ($this->_currentPage != $this->_totalPages) {
         $paginate['next'] = array('title' => 'Next &raquo;', 'url' => $this->current_url() . '?page=' . ($this->_currentPage + 1));
     }
     $context->set('paginate', $paginate);
     return parent::render($context);
 }
Example #11
0
 /**
  * Renders the current template
  *
  * @param array $assigns An array of values for the template
  * @param array $filters Additional filters for the template
  * @param array $registers Additional registers for the template
  * @return string
  */
 function render($assigns = null, $filters = null, $registers = null)
 {
     if (is_null($assigns)) {
         $assigns = array();
     }
     $context = new LiquidContext($assigns, $registers);
     if (!is_null($filters)) {
         if (is_array($filters)) {
             array_merge($this->filters, $filters);
         } else {
             $this->filters[] = $filters;
         }
     }
     foreach ($this->filters as $filter) {
         $context->add_filters($filter);
     }
     return $this->root->render($context);
 }
 /**
  * Renders the tag
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     $context->set($this->_to, $context->get($this->_from));
 }
 /**
  * Renders the node
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     $context->push();
     $result = $this->_document->render($context);
     $context->pop();
     return $result;
 }
Example #14
0
 function test_strip_html()
 {
     $var = new LiquidVariable("var | strip_html");
     $this->context->set('var', "<b>bla blub</a>");
     $this->assertEqual("bla blub", $var->render($this->context));
 }
 /**
  * Renders the node
  *
  * @param LiquidContext $context
  */
 public function render(&$context)
 {
     $result = '';
     $variable = $context->get($this->_variable);
     $context->push();
     foreach ($this->_attributes as $key => $value) {
         $context->set($key, $context->get($value));
     }
     if ($this->_collection) {
         foreach ($variable as $item) {
             $context->set($this->_templateName, $item);
             $result .= $this->_document->render($context);
         }
     } else {
         if (!is_null($this->_variable)) {
             $context->set($this->_templateName, $variable);
         }
         $result .= $this->_document->render($context);
     }
     $context->pop();
     return $result;
 }