Ejemplo n.º 1
0
 /**
  * Constructor
  *
  * @param string $markup
  * @param array $tokens
  * @param FileSystem $fileSystem
  *
  * @throws \Liquid\LiquidException
  */
 public function __construct($markup, array &$tokens, FileSystem $fileSystem = null)
 {
     $syntaxRegexp = new Regexp('/(\\w+)\\s*=\\s*(' . Liquid::get('QUOTED_FRAGMENT') . '+)/');
     $filterSeperatorRegexp = new Regexp('/' . Liquid::get('FILTER_SEPARATOR') . '\\s*(.*)/');
     $filterSplitRegexp = new Regexp('/' . Liquid::get('FILTER_SEPARATOR') . '/');
     $filterNameRegexp = new Regexp('/\\s*(\\w+)/');
     $filterArgumentRegexp = new Regexp('/(?:' . Liquid::get('FILTER_ARGUMENT_SEPARATOR') . '|' . Liquid::get('ARGUMENT_SEPARATOR') . ')\\s*(' . Liquid::get('QUOTED_FRAGMENT') . ')/');
     $this->filters = array();
     if ($filterSeperatorRegexp->match($markup)) {
         $filters = $filterSplitRegexp->split($filterSeperatorRegexp->matches[1]);
         foreach ($filters as $filter) {
             $filterNameRegexp->match($filter);
             $filtername = $filterNameRegexp->matches[1];
             $filterArgumentRegexp->matchAll($filter);
             $matches = Liquid::arrayFlatten($filterArgumentRegexp->matches[1]);
             array_push($this->filters, array($filtername, $matches));
         }
     }
     if ($syntaxRegexp->match($markup)) {
         $this->to = $syntaxRegexp->matches[1];
         $this->from = $syntaxRegexp->matches[2];
     } else {
         throw new LiquidException("Syntax Error in 'assign' - Valid syntax: assign [var] = [source]");
     }
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }