Skip to content

Commit

Permalink
Added {% script %} and {% style %} tags for Twig templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Oct 31, 2017
1 parent 586fcf4 commit 84789cb
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Added `Grav\Framework\Page` interfaces
* Added `|nicenumber` Twig filter
* Added `{% try %} ... {% catch %} Error: {{ e.message }} {% endcatch %}` tag to allow basic exception handling inside Twig
* Added `{% script %}` and `{% style %}` tags for Twig templates
* Deprecated GravTrait
1. [](#improved)
* Make it possible to include debug bar also into non-HTML responses
Expand Down
101 changes: 101 additions & 0 deletions system/src/Grav/Common/Twig/Node/TwigNodeScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* @package Grav.Common.Twig
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Twig\Node;

class TwigNodeScript extends \Twig_Node implements \Twig_NodeOutputInterface
{
protected $tagName = 'script';

/**
* TwigNodeScript constructor.
* @param \Twig_NodeInterface|null $body
* @param \Twig_Node_Expression|null $file
* @param \Twig_Node_Expression|null $group
* @param \Twig_Node_Expression|null $priority
* @param \Twig_Node_Expression|null $attributes
* @param int $lineno
* @param string|null $tag
*/
public function __construct(
\Twig_NodeInterface $body = null,
\Twig_Node_Expression $file = null,
\Twig_Node_Expression $group = null,
\Twig_Node_Expression $priority = null,
\Twig_Node_Expression $attributes = null,
$lineno,
$tag = null
)
{
parent::__construct(['body' => $body, 'file' => $file, 'group' => $group, 'priority' => $priority, 'attributes' => $attributes], [], $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param \Twig_Compiler $compiler A Twig_Compiler instance
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);

if ($this->getNode('attributes') !== null) {
$compiler
->write('$attributes = ')
->subcompile($this->getNode('attributes'))
->raw(";\n")
->write("if (\$attributes !== null && !is_array(\$attributes)) {\n")
->indent()
->write("throw new UnexpectedValueException('{% {$this->tagName} with x %}: x is not an array');\n")
->outdent()
->write("}\n");
} else {
$compiler->write('$attributes = [];' . "\n");
}

if ($this->getNode('group') !== null) {
$compiler
->write('$group = ')
->subcompile($this->getNode('group'))
->raw(";\n")
->write("if (\$group !== null && !is_string(\$group)) {\n")
->indent()
->write("throw new UnexpectedValueException('{% {$this->tagName} in x %}: x is not a string');\n")
->outdent()
->write("}\n");
} else {
$compiler->write('$group = null;' . "\n");
}

if ($this->getNode('priority') !== null) {
$compiler
->write('$priority = (int)(')
->subcompile($this->getNode('priority'))
->raw(");\n");
} else {
$compiler->write('$priority = null;' . "\n");
}

$compiler->write("\$assets = \\Grav\\Common\\Grav::instance()['assets'];\n");

if ($this->getNode('file') !== null) {
$compiler
->write('$file = ')
->subcompile($this->getNode('file'))
->write(";\n")
->write("\$pipeline = !empty(\$attributes['pipeline']);\n")
->write("\$loading = !empty(\$attributes['defer']) ? 'defer' : (!empty(\$attributes['async']) ? 'async' : null);\n")
->write("\$assets->addJs(\$file, \$priority, \$pipeline, \$loading, \$group);\n");
} else {
$compiler
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->write("\$content = ob_end_clean();")
->write("\$assets->addInlineJs(\$content, \$priority, \$group, \$attributes);\n");
}
}
}
97 changes: 97 additions & 0 deletions system/src/Grav/Common/Twig/Node/TwigNodeStyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* @package Grav.Common.Twig
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Twig\Node;

class TwigNodeStyle extends \Twig_Node implements \Twig_NodeOutputInterface
{
protected $tagName = 'style';

/**
* TwigNodeAssets constructor.
* @param \Twig_NodeInterface|null $body
* @param \Twig_Node_Expression|null $attributes
* @param int $lineno
* @param null $tag
*/
public function __construct(
\Twig_NodeInterface $body = null,
\Twig_Node_Expression $file = null,
\Twig_Node_Expression $group = null,
\Twig_Node_Expression $priority = null,
\Twig_Node_Expression $attributes = null,
$lineno,
$tag = null
)
{
parent::__construct(['body' => $body, 'file' => $file, 'group' => $group, 'priority' => $priority, 'attributes' => $attributes], [], $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param \Twig_Compiler $compiler A Twig_Compiler instance
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);

if ($this->getNode('attributes') !== null) {
$compiler
->write('$attributes = ')
->subcompile($this->getNode('attributes'))
->raw(";\n")
->write("if (\$attributes !== null && !is_array(\$attributes)) {\n")
->indent()
->write("throw new UnexpectedValueException('{% {$this->tagName} with x %}: x is not an array');\n")
->outdent()
->write("}\n");
} else {
$compiler->write('$attributes = [];' . "\n");
}

if ($this->getNode('group') !== null) {
$compiler
->write('$group = ')
->subcompile($this->getNode('group'))
->raw(";\n")
->write("if (\$group !== null && !is_string(\$group)) {\n")
->indent()
->write("throw new UnexpectedValueException('{% {$this->tagName} in x %}: x is not a string');\n")
->outdent()
->write("}\n");
} else {
$compiler->write('$group = null;' . "\n");
}

if ($this->getNode('priority') !== null) {
$compiler
->write('$priority = (int)(')
->subcompile($this->getNode('priority'))
->raw(");\n");
} else {
$compiler->write('$priority = null;' . "\n");
}

$compiler->write("\$assets = \\Grav\\Common\\Grav::instance()['assets'];\n");

if ($this->getNode('file') !== null) {
$compiler
->write('$file = ')
->subcompile($this->getNode('file'))
->write(";\n")
->write("\$pipeline = !empty(\$attributes['pipeline']);\n")
->write("\$assets->addCss(\$file, \$priority, \$pipeline, \$group);\n");
} else {
$compiler
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->write("\$content = ob_end_clean();")
->write("\$assets->addInlineCss(\$content, \$priority, \$group);\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Twig;
namespace Grav\Common\Twig\Node;

class TwigNodeTry extends \Twig_Node
class TwigNodeTryCatch extends \Twig_Node
{
public function __construct(\Twig_NodeInterface $try, \Twig_NodeInterface $catch = null, $lineno, $tag = null)
{
Expand Down
105 changes: 105 additions & 0 deletions system/src/Grav/Common/Twig/TokenParser/TwigTokenParserScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* @package Grav.Common.Twig
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Twig\TokenParser;

use Grav\Common\Twig\Node\TwigNodeScript;

/**
* Adds a script to head/bottom/custom location in the document.
*
* {% script 'theme://js/something.js' in 'bottom' priority 20 with { defer: true, async: true } %}
*
* {% script in 'bottom' priority 20 %}
* alert('Warning!');
* {% endscript %}
*/
class TwigTokenParserScript extends \Twig_TokenParser
{
/**
* Parses a token and returns a node.
*
* @param \Twig_Token $token A Twig_Token instance
*
* @return \Twig_NodeInterface A Twig_NodeInterface instance
*/
public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();

list($file, $group, $priority, $attributes) = $this->parseArguments($token);

$content = null;
if ($file === null) {
$content = $this->parser->subparse([$this, 'decideBlockEnd'], true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
}

return new TwigNodeScript($content, $file, $group, $priority, $attributes, $lineno, $this->getTag());
}

/**
* @param \Twig_Token $token
* @return array
*/
protected function parseArguments(\Twig_Token $token)
{
$stream = $this->parser->getStream();

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

return [null, null, null, null];
}

$file = null;
if (!$stream->nextIf([\Twig_Token::NAME_TYPE, \Twig_Token::OPERATOR_TYPE])) {
$file = $this->parser->getExpressionParser()->parseExpression();
}

$group = null;
if ($stream->nextIf(\Twig_Token::OPERATOR_TYPE, 'in')) {
$group = $this->parser->getExpressionParser()->parseExpression();
}

$priority = null;
if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'priority')) {
$priority = $this->parser->getExpressionParser()->parseExpression();
}

$attributes = null;
if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'with')) {
$attributes = $this->parser->getExpressionParser()->parseExpression();
}

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

return [$file, $group, $priority, $attributes];
}

/**
* @param \Twig_Token $token
* @return bool
*/
public function decideBlockEnd(\Twig_Token $token)
{
return $token->test('endscript');
}

/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'script';
}
}
Loading

0 comments on commit 84789cb

Please sign in to comment.