Ejemplo n.º 1
0
 /**
  * Renders the tag
  *
  * @param Context $context
  *
  * @return string
  *
  */
 public function render(Context $context)
 {
     $this->currentPage = is_numeric($context->get('page')) ? $context->get('page') : 1;
     $this->currentOffset = ($this->currentPage - 1) * $this->numberItems;
     $this->collection = $context->get($this->collectionName);
     $this->collectionSize = count($this->collection);
     $this->totalPages = ceil($this->collectionSize / $this->numberItems);
     $paginatedCollection = 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] => $paginatedCollection));
     } else {
         $context->set($this->collectionName, $paginatedCollection);
     }
     $paginate = array('page_size' => $this->numberItems, 'current_page' => $this->currentPage, 'current_offset' => $this->currentOffset, 'pages' => $this->totalPages, 'items' => $this->collectionSize);
     if ($this->currentPage != 1) {
         $paginate['previous']['title'] = 'Previous';
         $paginate['previous']['url'] = $this->currentUrl($context) . '?page=' . ($this->currentPage - 1);
     }
     if ($this->currentPage != $this->totalPages) {
         $paginate['next']['title'] = 'Next';
         $paginate['next']['url'] = $this->currentUrl($context) . '?page=' . ($this->currentPage + 1);
     }
     $context->set('paginate', $paginate);
     return parent::render($context);
 }
Ejemplo n.º 2
0
 /**
  * Renders the tag
  *
  * @param Context $context
  *
  * @return null|string
  */
 public function render(Context $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 = isset($this->attributes['limit']) ? $context->get($this->attributes['limit']) : null;
                 $rangeEnd = $limit ? $limit : count($collection) - $offset;
                 $range = array($offset, $rangeEnd);
                 $context->registers['for'][$this->name] = $rangeEnd + $offset;
             }
             $result = '';
             $segment = array_slice($collection, $range[0], $range[1]);
             if (!count($segment)) {
                 return null;
             }
             $context->push();
             $length = count($segment);
             $index = 0;
             foreach ($segment as $key => $item) {
                 $value = is_numeric($key) ? $item : array($key, $item);
                 $context->set($this->variableName, $value);
                 $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;
         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;
 }
Ejemplo n.º 3
0
 /**
  * Renders the current node
  *
  * @param Context $context
  *
  * @return string
  */
 public function render(Context $context)
 {
     $collection = $context->get($this->collectionName);
     if (!is_array($collection)) {
         die('not array, ' . var_export($collection, true));
     }
     // 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->variableName, $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->renderAll($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;
 }
Ejemplo n.º 4
0
 /**
  * Renders the tag
  *
  * @param Context $context
  *
  * @return null|string
  */
 public function render(Context $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 = isset($this->attributes['limit']) ? $context->get($this->attributes['limit']) : null;
         $rangeEnd = $limit ? $limit : count($collection) - $offset;
         $range = array($offset, $rangeEnd);
         $context->registers['for'][$this->name] = $rangeEnd + $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;
 }
Ejemplo n.º 5
0
 /**
  * Renders the tag
  *
  * @param Context $context
  *
  * @return string|void
  */
 public function render(Context $context)
 {
     $output = $context->get($this->from);
     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);
     }
     $context->set($this->to, $output, true);
 }
Ejemplo n.º 6
0
 /**
  * Renders the node
  *
  * @param Context $context
  *
  * @return string
  */
 public function render(Context $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;
 }