/** * 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; }
/** * 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 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' => '« Previous', 'url' => $this->current_url() . '?page=' . ($this->_currentPage - 1)); } if ($this->_currentPage != $this->_totalPages) { $paginate['next'] = array('title' => 'Next »', 'url' => $this->current_url() . '?page=' . ($this->_currentPage + 1)); } $context->set('paginate', $paginate); return parent::render($context); }
/** * Renders the tag * * @param LiquidContext $context */ public function render(&$context) { if (!isset($context->registers['for'])) { $context->registers['for'] = array(); } switch ($this->_type) { case 'collection': $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); } break; case 'digit': $start = $this->_start; if (!is_integer($this->_start)) { $start = $context->get($this->_start); } $end = $this->_collectionName; if (!is_integer($this->_collectionName)) { $end = $context->get($this->_collectionName); } $range = array($start, $end); $context->push(); $result = ''; $index = 0; $length = $range[1] - $range[0]; for ($i = $range[0]; $i <= $range[1]; $i++) { $context->set($this->_variableName, $i); $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); $index++; } break; } $context->pop(); return $result; }
function test_hierchal_data() { $this->context->set('hash', array('name' => 'tobi')); $this->assertEqual('tobi', $this->context->get('hash.name')); }
/** * Renders the tag * * @param LiquidContext $context */ public function render(&$context) { $context->set($this->_to, $context->get($this->_from)); }
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; }