Skip to content

Commit

Permalink
Added {% switch %} twig tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 22, 2018
1 parent 8750602 commit e533024
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Added `$grav['uri]->getCurrentUri()` method to get `Grav\Framework\Uri\Uri` instance for the current URL
* Added `$grav['uri]->getCurrentRoute()` method to get `Grav\Framework\Route\Route` instance for the current URL
* Added ability to have `php` version dependencies in GPM assets
* Added new `{% switch %}` twig tag for more elegant if statements
* Added new `{% markdown %}` twig tag
1. [](#improved)
* Vendor library updated to latest
Expand Down
71 changes: 71 additions & 0 deletions system/src/Grav/Common/Twig/Node/TwigNodeSwitch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* @package Grav.Common.Twig
*
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Twig\Node;

class TwigNodeSwitch extends \Twig_Node implements \Twig_NodeOutputInterface
{
public function __construct(\Twig_NodeInterface $value, \Twig_NodeInterface $cases, \Twig_NodeInterface $default = null, $lineno, $tag = null)
{
parent::__construct(array('value' => $value, 'cases' => $cases, 'default' => $default), array(), $lineno, $tag);
}

/**
* Compiles the node to PHP.
*
* @param \Twig_Compiler A Twig_Compiler instance
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("switch (")
->subcompile($this->getNode('value'))
->raw(") {\n")
->indent();

foreach ($this->getNode('cases') as $case)
{
if (!$case->hasNode('body'))
{
continue;
}

foreach ($case->getNode('values') as $value)
{
$compiler
->write('case ')
->subcompile($value)
->raw(":\n");
}

$compiler
->write("{\n")
->indent()
->subcompile($case->getNode('body'))
->write("break;\n")
->outdent()
->write("}\n");
}

if ($this->hasNode('default') && $this->getNode('default') !== null)
{
$compiler
->write("default:\n")
->write("{\n")
->indent()
->subcompile($this->getNode('default'))
->outdent()
->write("}\n");
}

$compiler
->outdent()
->write("}\n");
}
}
138 changes: 138 additions & 0 deletions system/src/Grav/Common/Twig/TokenParser/TwigTokenParserSwitch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* @package Grav.Common.Twig
*
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
* @origin https://gist.github.com/maxgalbu/9409182
*/

namespace Grav\Common\Twig\TokenParser;

use Grav\Common\Twig\Node\TwigNodeSwitch;

/**
* Adds ability use elegant switch instead of ungainly if statements
*
* {% switch type %}
* {% case 'foo' %}
* {{ my_data.foo }}
* {% case 'bar' %}
* {{ my_data.bar }}
* {% default %}
* {{ my_data.default }}
* {% endswitch %}
*/
class TwigTokenParserSwitch extends \Twig_TokenParser
{
/**
* {@inheritdoc}
*/
public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();

$name = $this->parser->getExpressionParser()->parseExpression();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);

// There can be some whitespace between the {% switch %} and first {% case %} tag.
while ($stream->getCurrent()->getType() == \Twig_Token::TEXT_TYPE && trim($stream->getCurrent()->getValue()) == '')
{
$stream->next();
}

$stream->expect(\Twig_Token::BLOCK_START_TYPE);

$expressionParser = $this->parser->getExpressionParser();

$default = null;
$cases = array();
$end = false;

while (!$end)
{
$next = $stream->next();

switch ($next->getValue())
{
case 'case':
{
$values = array();

while (true)
{
$values[] = $expressionParser->parsePrimaryExpression();
// Multiple allowed values?
if ($stream->test(\Twig_Token::OPERATOR_TYPE, 'or'))
{
$stream->next();
}
else
{
break;
}
}

$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideIfFork'));
$cases[] = new \Twig_Node(array(
'values' => new \Twig_Node($values),
'body' => $body
));
break;
}
case 'default':
{
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$default = $this->parser->subparse(array($this, 'decideIfEnd'));
break;
}
case 'endswitch':
{
$end = true;
break;
}
default:
{
throw new \Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line %d)', $lineno), -1);
}
}
}

$stream->expect(\Twig_Token::BLOCK_END_TYPE);

return new TwigNodeSwitch($name, new \Twig_Node($cases), $default, $lineno, $this->getTag());
}

/**
* Decide if current token marks switch logic.
*
* @param \Twig_Token $token
* @return bool
*/
public function decideIfFork(\Twig_Token $token)
{
return $token->test(array('case', 'default', 'endswitch'));
}

/**
* Decide if current token marks end of swtich block.
*
* @param \Twig_Token $token
* @return bool
*/
public function decideIfEnd(\Twig_Token $token)
{
return $token->test(array('endswitch'));
}


/**
* {@inheritdoc}
*/
public function getTag()
{
return 'switch';
}
}
2 changes: 2 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Grav\Common\Page\Media;
use Grav\Common\Twig\TokenParser\TwigTokenParserScript;
use Grav\Common\Twig\TokenParser\TwigTokenParserStyle;
use Grav\Common\Twig\TokenParser\TwigTokenParserSwitch;
use Grav\Common\Twig\TokenParser\TwigTokenParserTryCatch;
use Grav\Common\Twig\TokenParser\TwigTokenParserMarkdown;
use Grav\Common\Utils;
Expand Down Expand Up @@ -158,6 +159,7 @@ public function getTokenParsers()
new TwigTokenParserScript(),
new TwigTokenParserStyle(),
new TwigTokenParserMarkdown(),
new TwigTokenParserSwitch(),
];
}

Expand Down

0 comments on commit e533024

Please sign in to comment.