|
| 1 | +<?php |
| 2 | + |
| 3 | +// these files are loaded three times and we can't re-set a class |
| 4 | +if (!class_exists("Project_grid_Node", false)) { |
| 5 | + |
| 6 | + class Project_grid_Node extends Twig_Node { |
| 7 | + |
| 8 | + private $class = ""; |
| 9 | + |
| 10 | + public function setClass($class){ |
| 11 | + $this->class = $class; |
| 12 | + } |
| 13 | + |
| 14 | + |
| 15 | + public function compile(Twig_Compiler $compiler){ |
| 16 | + // $compiler->addDebugInfo($this); |
| 17 | + // $compiler->write("echo \"<div class='o-grid'>\";")->raw(PHP_EOL); |
| 18 | + // parent::compile($compiler); |
| 19 | + // $compiler->write("echo \"</div>\";")->raw(PHP_EOL); |
| 20 | + |
| 21 | + |
| 22 | + $compiler->addDebugInfo($this); |
| 23 | + $compiler->write("echo \"<div class='o-grid "); |
| 24 | + |
| 25 | + $class = " "; |
| 26 | + if($this->class instanceof \Twig_Node_Expression_Constant) { |
| 27 | + $class = preg_replace("/\b(lg|md|sm|xs)([0-9]+)\b/", 'col-$1-$2', $this->class->getAttribute("value")); |
| 28 | + } |
| 29 | + $compiler->raw($class); |
| 30 | + |
| 31 | + $compiler->raw("'>\";")->raw(PHP_EOL); |
| 32 | + parent::compile($compiler); |
| 33 | + $compiler->write("echo \"</div>\";")->raw(PHP_EOL); |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +// these files are loaded three times and we can't re-set a class |
| 42 | +if (!class_exists("Project_grid_TokenParser", false)) { |
| 43 | + |
| 44 | + class Project_grid_TokenParser extends Twig_TokenParser { |
| 45 | + |
| 46 | + public function parse(Twig_Token $token) |
| 47 | + { |
| 48 | + |
| 49 | + |
| 50 | + // $stream = $this->parser->getStream(); |
| 51 | + // $stream->expect(Twig_Token::BLOCK_END_TYPE); |
| 52 | + // $inheritanceIndex = 1; |
| 53 | + // $nodes = array(); |
| 54 | + // $classes = array(); |
| 55 | + // $returnNode = null; |
| 56 | + $inheritanceIndex = 1; |
| 57 | + |
| 58 | + $stream = $this->parser->getStream(); |
| 59 | + |
| 60 | + $nodes = array(); |
| 61 | + $classes = array(); |
| 62 | + $returnNode = null; |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + if($stream->test(Twig_Token::STRING_TYPE)) { |
| 67 | + $classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression(); |
| 68 | + } else { |
| 69 | + $classes[$inheritanceIndex] = "col-md-12"; |
| 70 | + } |
| 71 | + |
| 72 | + $stream->expect(Twig_Token::BLOCK_END_TYPE); |
| 73 | + $continue = true; |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + while($continue) { |
| 78 | + |
| 79 | + $content = $this->parser->subparse(array($this, 'decideMyTagFork')); |
| 80 | + $nodes[$inheritanceIndex][] = $content; |
| 81 | + $tag = $stream->next()->getValue(); |
| 82 | + // $stream->expect(Twig_Token::BLOCK_END_TYPE); |
| 83 | + switch($tag) { |
| 84 | + case "grid": |
| 85 | + // $inheritanceIndex++; |
| 86 | + // |
| 87 | + // if($stream->test(Twig_Token::STRING_TYPE)) { |
| 88 | + // $classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression(); |
| 89 | + // } else { |
| 90 | + // $classes[$inheritanceIndex] = "col-md-12"; |
| 91 | + // } |
| 92 | + // |
| 93 | + // break; |
| 94 | + $inheritanceIndex++; |
| 95 | + if($stream->test(Twig_Token::STRING_TYPE)) { |
| 96 | + $classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression(); |
| 97 | + } else { |
| 98 | + $classes[$inheritanceIndex] = "col-md-12"; |
| 99 | + } |
| 100 | + break; |
| 101 | + case "endgrid": |
| 102 | + // $currentNodes = $nodes[$inheritanceIndex]; |
| 103 | + // $class = $classes[$inheritanceIndex]; |
| 104 | + // unset($nodes[$inheritanceIndex]); |
| 105 | + // unset($classes[$inheritanceIndex]); |
| 106 | + // $inheritanceIndex--; |
| 107 | + // if($inheritanceIndex == 0) { |
| 108 | + // $returnNode = new Project_grid_Node($currentNodes); |
| 109 | + // $returnNode->setClass($class); |
| 110 | + // $continue = false; |
| 111 | + // // break 2; |
| 112 | + // } else { |
| 113 | + // $nodes[$inheritanceIndex][] = new Project_grid_Node($currentNodes); |
| 114 | + // $node->setClass($class); |
| 115 | + // $nodes[$inheritanceIndex][] = $node; |
| 116 | + // } |
| 117 | + // break; |
| 118 | + $currentNodes = $nodes[$inheritanceIndex]; |
| 119 | + $class = $classes[$inheritanceIndex]; |
| 120 | + unset($nodes[$inheritanceIndex]); |
| 121 | + unset($classes[$inheritanceIndex]); |
| 122 | + $inheritanceIndex--; |
| 123 | + if($inheritanceIndex == 0) { |
| 124 | + $returnNode = new Project_grid_Node($currentNodes); |
| 125 | + $returnNode->setClass($class); |
| 126 | + $continue = false; |
| 127 | + } else { |
| 128 | + $node = new Project_grid_Node($currentNodes); |
| 129 | + $node->setClass($class); |
| 130 | + $nodes[$inheritanceIndex][] = $node; |
| 131 | + } |
| 132 | + break; |
| 133 | + default: |
| 134 | + throw new Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "%s" to close the "%s" block started at line %d)', "endgrid", "grid", $this->startLine), -1); |
| 135 | + } |
| 136 | + $stream->expect(Twig_Token::BLOCK_END_TYPE); |
| 137 | + } |
| 138 | + |
| 139 | + return $returnNode; |
| 140 | + } |
| 141 | + |
| 142 | + public function getTag() |
| 143 | + { |
| 144 | + return "grid"; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Callback called at each tag name when subparsing, must return |
| 149 | + * true when the expected end tag is reached. |
| 150 | + * |
| 151 | + * @param \Twig_Token $token |
| 152 | + * @return bool |
| 153 | + */ |
| 154 | + public function decideMyTagFork(Twig_Token $token) |
| 155 | + { |
| 156 | + return $token->test(array("grid", "endgrid")); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +?> |
0 commit comments