Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Twig tag - switch

  • Loading branch information...
commit e1a1c17764a8db8091de2b5e1ec9abed6836899b 1 parent 0e6d963
@jusuff jusuff authored
View
8 src/Zikula/Bundle/CoreBundle/Twig/Extension/CoreExtension.php
@@ -2,6 +2,7 @@
namespace Zikula\Bundle\CoreBundle\Twig\Extension;
+use Zikula\Bundle\CoreBundle\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CoreExtension extends \Twig_Extension
@@ -13,6 +14,13 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}
+ public function getTokenParsers()
+ {
+ return array(
+ new Twig\TokenParser\SwitchTokenParser(),
+ );
+ }
+
/**
* Returns a list of functions to add to the existing list.
*
View
46 src/Zikula/Bundle/CoreBundle/Twig/Node/SwitchNode.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Zikula\Bundle\CoreBundle\Twig\Node;
+
+use Zikula\Bundle\CoreBundle\Twig;
+
+class SwitchNode extends \Twig_Node
+{
+ public function __construct($cases, \Twig_Node_Expression $value, $lineno, $tag = null)
+ {
+ parent::__construct(array('value' => $value), array('cases' => $cases), $lineno, $tag);
+ }
+
+ public function compile(\Twig_Compiler $compiler)
+ {
+ $compiler->addDebugInfo($this);
+ $compiler
+ ->write('switch (')
+ ->subcompile($this->getNode('value'))
+ ->raw(") {\n")
+ ->indent();
+
+ foreach ((array)$this->getAttribute('cases') as $key => $case) {
+ if ($key === 'default') {
+ $compiler
+ ->write('default');
+ } else {
+ $compiler
+ ->write('case ')
+ ->subcompile($case['expression']);
+ }
+ $compiler
+ ->raw(":\n")
+ ->indent()
+ ->subcompile($case['body']);
+ if (isset($case['break'])) {
+ $compiler
+ ->write("break;\n");
+ }
+ $compiler->outdent();
+ }
+ $compiler
+ ->outdent()
+ ->raw("}\n");
+ }
+}
View
98 src/Zikula/Bundle/CoreBundle/Twig/TokenParser/SwitchTokenParser.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Zikula\Bundle\CoreBundle\Twig\TokenParser;
+
+use Zikula\Bundle\CoreBundle\Twig;
+
+/**
+ * @example
+ * {% switch variable %}
+ * {% case val_1 %}
+ * code for val_1
+ * (notice - here's not break)
+ * {% case val_2 %}
+ * code for val_2
+ * {% break %}
+ * {% default %}
+ * code for default case
+ * {% endswitch %}
+ *
+ */
+class SwitchTokenParser extends \Twig_TokenParser
+{
+ public function parse(\Twig_Token $token)
+ {
+ $lineno = $token->getLine();
+
+ $value = $this->parser->getExpressionParser()->parseExpression();
+
+ $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
+ $this->parser->subparse(array($this, 'decideCaseFork'));
+ $cases = array();
+
+ $end = false;
+ $i = 0;
+ while (!$end) {
+ switch ($tag = $this->parser->getStream()->next()->getValue()) {
+ case 'case':
+ $i++;
+ $expr = $this->parser->getExpressionParser()->parseExpression();
+ $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
+ $body = $this->parser->subparse(array($this, 'decideCaseFork'));
+
+ $cases[$i] = array(
+ 'expression' => $expr,
+ 'body' => $body,
+ );
+
+ break;
+
+ case 'default':
+ $i = null;
+ $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
+ $body = $this->parser->subparse(array($this, 'decideCaseFork'));
+
+ $cases['default'] = array(
+ 'body' => $body,
+ );
+
+ break;
+
+ case 'break':
+ $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
+ $this->parser->subparse(array($this, 'decideCaseFork'));
+ if (!is_null($i)) {
+ $cases[$i]['break'] = true;
+ }
+ break;
+
+ case 'endcase':
+ case 'enddefault':
+ $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
+ $this->parser->subparse(array($this, 'decideCaseFork'));
+ break;
+
+ case 'endswitch':
+ $end = true;
+ break;
+
+ default:
+ throw new \Twig_Error_Syntax(sprintf('Unexpected end of template at line %d' . $tag, $lineno), -1);
+ }
+ }
+
+ $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
+
+ return new Twig\Node\SwitchNode($cases, $value, $lineno, $this->getTag());
+ }
+
+ public function decideCaseFork(\Twig_Token $token)
+ {
+ return $token->test(array('case', 'endcase', 'default', 'enddefault', 'break', 'endswitch'));
+ }
+
+ public function getTag()
+ {
+ return 'switch';
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.