mirror of
https://github.com/getgrav/grav.git
synced 2026-05-07 03:27:08 +02:00
Added {% script %} and {% style %} tags for Twig templates
This commit is contained in:
@@ -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
|
||||
|
||||
101
system/src/Grav/Common/Twig/Node/TwigNodeScript.php
Normal file
101
system/src/Grav/Common/Twig/Node/TwigNodeScript.php
Normal file
@@ -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
system/src/Grav/Common/Twig/Node/TwigNodeStyle.php
Normal file
97
system/src/Grav/Common/Twig/Node/TwigNodeStyle.php
Normal file
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?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\TwigNodeStyle;
|
||||
|
||||
/**
|
||||
* Adds a style to the document.
|
||||
*
|
||||
* {% style 'theme://css/foo.css' priority 20 %}
|
||||
|
||||
* {% style priority 20 with { media: 'screen' } %}
|
||||
* a { color: red; }
|
||||
* {% endstyle %}
|
||||
*/
|
||||
class TwigTokenParserStyle 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) {
|
||||
$content = $this->parser->subparse([$this, 'decideBlockEnd'], true);
|
||||
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
||||
}
|
||||
|
||||
return new TwigNodeStyle($content, $file, $group, $priority, $attributes, $lineno, $this->getTag());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Twig_Token $token
|
||||
* @return array
|
||||
*/
|
||||
protected function parseArguments(\Twig_Token $token)
|
||||
{
|
||||
$stream = $this->parser->getStream();
|
||||
|
||||
$file = null;
|
||||
if (!$stream->nextIf([\Twig_Token::NAME_TYPE, \Twig_Token::OPERATOR_TYPE, \Twig_Token::BLOCK_END_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('endstyle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tag name associated with this token parser.
|
||||
*
|
||||
* @return string The tag name
|
||||
*/
|
||||
public function getTag()
|
||||
{
|
||||
return 'style';
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,9 @@
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Common\Twig;
|
||||
namespace Grav\Common\Twig\TokenParser;
|
||||
|
||||
use Grav\Common\Twig\Node\TwigNodeTryCatch;
|
||||
|
||||
/**
|
||||
* Handles try/catch in template file.
|
||||
@@ -19,7 +21,7 @@ namespace Grav\Common\Twig;
|
||||
* {% endcatch %}
|
||||
* </pre>
|
||||
*/
|
||||
class TokenParserTry extends \Twig_TokenParser
|
||||
class TwigTokenParserTryCatch extends \Twig_TokenParser
|
||||
{
|
||||
/**
|
||||
* Parses a token and returns a node.
|
||||
@@ -41,7 +43,7 @@ class TokenParserTry extends \Twig_TokenParser
|
||||
$stream->next();
|
||||
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
|
||||
|
||||
return new TwigNodeTry($try, $catch, $lineno, $this->getTag());
|
||||
return new TwigNodeTryCatch($try, $catch, $lineno, $this->getTag());
|
||||
}
|
||||
|
||||
public function decideCatch(\Twig_Token $token)
|
||||
@@ -11,6 +11,9 @@ namespace Grav\Common\Twig;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Page\Collection;
|
||||
use Grav\Common\Page\Media;
|
||||
use Grav\Common\Twig\TokenParser\TwigTokenParserScript;
|
||||
use Grav\Common\Twig\TokenParser\TwigTokenParserStyle;
|
||||
use Grav\Common\Twig\TokenParser\TwigTokenParserTryCatch;
|
||||
use Grav\Common\Utils;
|
||||
use Grav\Common\Markdown\Parsedown;
|
||||
use Grav\Common\Markdown\ParsedownExtra;
|
||||
@@ -18,7 +21,7 @@ use Grav\Common\Uri;
|
||||
use Grav\Common\Helpers\Base32;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
|
||||
class TwigExtension extends \Twig_Extension
|
||||
class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
|
||||
{
|
||||
protected $grav;
|
||||
protected $debugger;
|
||||
@@ -34,16 +37,6 @@ class TwigExtension extends \Twig_Extension
|
||||
$this->config = $this->grav['config'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns extension name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'GravTwigExtension';
|
||||
}
|
||||
|
||||
/**
|
||||
* Register some standard globals
|
||||
*
|
||||
@@ -154,7 +147,9 @@ class TwigExtension extends \Twig_Extension
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return [
|
||||
new TokenParserTry(),
|
||||
new TwigTokenParserTryCatch(),
|
||||
new TwigTokenParserScript(),
|
||||
new TwigTokenParserStyle(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user