/** * 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; }
/** * Render the tag * * @param Context $context * * @throws \Liquid\LiquidException * @return string */ public function render(Context $context) { $context->push(); $logicalRegex = new Regexp('/\\s+(and|or)\\s+/'); $conditionalRegex = new Regexp('/(' . Liquid::get('QUOTED_FRAGMENT') . ')\\s*([=!<>a-z_]+)?\\s*(' . Liquid::get('QUOTED_FRAGMENT') . ')?/'); $result = ''; foreach ($this->blocks as $block) { if ($block[0] == 'else') { $result = $this->renderAll($block[2], $context); break; } if ($block[0] == 'if' || $block[0] == 'elsif') { // Extract logical operators $logicalRegex->matchAll($block[1]); $logicalOperators = $logicalRegex->matches; $logicalOperators = $logicalOperators[1]; // 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 = $this->interpretCondition($conditions[0]['left'], $conditions[0]['right'], $conditions[0]['operator'], $context); foreach ($logicalOperators as $k => $logicalOperator) { if ($logicalOperator == 'and') { $display = $display && $this->interpretCondition($conditions[$k + 1]['left'], $conditions[$k + 1]['right'], $conditions[$k + 1]['operator'], $context); } else { $display = $display || $this->interpretCondition($conditions[$k + 1]['left'], $conditions[$k + 1]['right'], $conditions[$k + 1]['operator'], $context); } } } else { // If statement is a single condition $display = $this->interpretCondition($conditions[0]['left'], $conditions[0]['right'], $conditions[0]['operator'], $context); } if ($display) { $result = $this->renderAll($block[2], $context); break; } } } $context->pop(); return $result; }
/** * 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); }
/** * Renders the tag * * @param Context $context * * @return string|void */ public function render(Context $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 $fromContext = $context->get($this->toDecrement); // we already have a value in the context $context->environments[0][$this->toDecrement] = null !== $fromContext ? $fromContext : 0; } // decrement the environment value $context->environments[0][$this->toDecrement]--; return ''; }
/** * Renders the tag * * @var Context $context * @return string */ public function render(Context $context) { $context->push(); $key = $context->get($this->name); if (isset($context->registers['cycle'][$key])) { $iteration = $context->registers['cycle'][$key]; } else { $iteration = 0; } $result = $context->get($this->variables[$iteration]); $iteration += 1; if ($iteration >= count($this->variables)) { $iteration = 0; } $context->registers['cycle'][$key] = $iteration; $context->pop(); return $result; }
public function __construct(View $viewer) { $this->viewer = $viewer; $this->controller = $viewer->getController(); if ($this->controller) { $this->record = $this->controller->getRecord(); } parent::__construct(); }
/** * 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; }
/** * Returns the current page URL * * @param Context $context * * @return string * */ public function currentUrl($context) { $uri = explode('?', $context->get('REQUEST_URI')); $url = 'http'; if ($context->get('HTTPS') == 'on') { $url .= 's'; } $url .= '://' . $context->get('HTTP_HOST') . reset($uri); return $url; }
/** * 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; }
/** * Renders the node * * @param Context $context * * @return string */ public function render(Context $context) { $context->push(); $result = $this->document->render($context); $context->pop(); return $result; }
/** * 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; }