-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
{% script %}
and {% style %}
tags for Twig templates
- Loading branch information
Showing
8 changed files
with
416 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
system/src/Grav/Common/Twig/TokenParser/TwigTokenParserScript.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
Oops, something went wrong.