From 182c7eb04c7f3ee90a4eb4a98704a2ef7386bbff Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sun, 8 May 2011 21:56:32 +0200 Subject: [PATCH] General update Update syntax to add and display assets: Before: {% javascript 'js/jquery/jquery.js' %} {% javascript 'js/main.js' %} {% stylesheet 'css/main.css' %} {% stylesheet 'css/form.css' %} {% javascripts %} {% stylesheets %} After: {{ javascript(['js/jquery/jquery.js', 'js/main.js']) }} {{ stylesheet(['css/main.css', 'css/form.css']) }} {{ javascripts }} {{ stylesheets }} --- README.markdown | 20 ++------ Templating/Helper/JavascriptsHelper.php | 6 ++- Templating/Helper/StylesheetsHelper.php | 6 ++- Twig/Extension/AssetsExtension.php | 46 +++++++++-------- Twig/Node/JavascriptNode.php | 42 --------------- Twig/Node/JavascriptsNode.php | 38 -------------- Twig/Node/StylesheetNode.php | 42 --------------- Twig/Node/StylesheetsNode.php | 38 -------------- Twig/TokenParser/JavascriptTokenParser.php | 57 --------------------- Twig/TokenParser/JavascriptsTokenParser.php | 46 ----------------- Twig/TokenParser/StylesheetTokenParser.php | 57 --------------------- Twig/TokenParser/StylesheetsTokenParser.php | 46 ----------------- 12 files changed, 38 insertions(+), 406 deletions(-) delete mode 100644 Twig/Node/JavascriptNode.php delete mode 100644 Twig/Node/JavascriptsNode.php delete mode 100644 Twig/Node/StylesheetNode.php delete mode 100644 Twig/Node/StylesheetsNode.php delete mode 100644 Twig/TokenParser/JavascriptTokenParser.php delete mode 100644 Twig/TokenParser/JavascriptsTokenParser.php delete mode 100644 Twig/TokenParser/StylesheetTokenParser.php delete mode 100644 Twig/TokenParser/StylesheetsTokenParser.php diff --git a/README.markdown b/README.markdown index 44dea11..0f5efd8 100644 --- a/README.markdown +++ b/README.markdown @@ -98,20 +98,8 @@ The files merged can be minify. ### Twig - {% javascript 'js/jquery/jquery.js' %} - {% javascript 'js/main.js' %} - {% stylesheet 'css/main.css' %} - {% stylesheet 'css/form.css' %} + {{ javascript(['js/jquery/jquery.js', 'js/main.js'] }} + {{ stylesheet(['css/main.css', 'css/form.css') }} - {% javascripts %} - {% stylesheets %} - -### PHP - - add('js/jquery/jquery.js') ?> - add('js/main.js') ?> - add('css/main.css') ?> - add('css/form.css') ?> - - - + {{ javascripts }} + {{ stylesheets }} diff --git a/Templating/Helper/JavascriptsHelper.php b/Templating/Helper/JavascriptsHelper.php index a6de80a..ceea2e6 100644 --- a/Templating/Helper/JavascriptsHelper.php +++ b/Templating/Helper/JavascriptsHelper.php @@ -53,9 +53,11 @@ public function __construct(AssetsHelper $assetHelper, Minifier $minifier, array * @param string $javascript A javascript file path * @param array $attributes An array of attributes */ - public function add($javascript, $attributes = array()) + public function add(array $javascripts, $attributes = array()) { - $this->javascripts[$javascript] = $attributes; + foreach ($javascripts as $javascript) { + $this->javascripts[$javascript] = $attributes; + } } /** diff --git a/Templating/Helper/StylesheetsHelper.php b/Templating/Helper/StylesheetsHelper.php index f519fc9..7a61b99 100644 --- a/Templating/Helper/StylesheetsHelper.php +++ b/Templating/Helper/StylesheetsHelper.php @@ -53,9 +53,11 @@ public function __construct(AssetsHelper $assetHelper, MinifierInterface $minifi * @param string $stylesheet A stylesheet file path * @param array $attributes An array of attributes */ - public function add($stylesheet, $attributes = array()) + public function add($stylesheets, $attributes = array()) { - $this->stylesheets[$stylesheet] = $attributes; + foreach ($stylesheets as $stylesheet) { + $this->stylesheets[$stylesheet] = $attributes; + } } /** diff --git a/Twig/Extension/AssetsExtension.php b/Twig/Extension/AssetsExtension.php index ea6f926..2f4165c 100644 --- a/Twig/Extension/AssetsExtension.php +++ b/Twig/Extension/AssetsExtension.php @@ -30,36 +30,42 @@ public function __construct(ContainerInterface $container) $this->container = $container; } - public function getContainer() + public function getFunctions() { - return $this->container; + return array( + 'stylesheet' => new \Twig_Function_Method($this, 'addStylesheet'), + 'stylesheets' => new \Twig_Function_Method($this, 'renderStylesheet', array('is_safe' => array('html'))), + 'javascript' => new \Twig_Function_Method($this, 'addJavascript'), + 'javascripts' => new \Twig_Function_Method($this, 'renderJavascript', array('is_safe' => array('html'))), + ); } - public function getTemplating() + public function addStylesheet($stylesheets) { - return $this->container->get('templating.engine'); + if (!is_array($stylesheets)) { + $stylesheets = array($stylesheets); + } + + $this->container->get('templating.helper.stylesheets')->add($stylesheets); } - /** - * Returns the token parser instance to add to the existing list. - * - * @return array An array of Twig_TokenParser instances - */ - public function getTokenParsers() + public function renderStylesheet() { - return array( - // {% javascript 'bundles/blog/js/blog.js' %} - new JavascriptTokenParser(), + return $this->container->get('templating.helper.stylesheets')->render(); + } - // {% javascripts %} - new JavascriptsTokenParser(), + public function addJavascript($javascripts) + { + if (!is_array($javascripts)) { + $javascripts = array($javascripts); + } - // {% stylesheet 'bundles/blog/css/blog.css' with { 'media': 'screen' } %} - new StylesheetTokenParser(), + $this->container->get('templating.helper.javascripts')->add($javascripts); + } - // {% stylesheets %} - new StylesheetsTokenParser(), - ); + public function renderJavascript() + { + return $this->container->get('templating.helper.javascripts')->render(); } /** diff --git a/Twig/Node/JavascriptNode.php b/Twig/Node/JavascriptNode.php deleted file mode 100644 index 3e3f58f..0000000 --- a/Twig/Node/JavascriptNode.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\Node; - -/** - * Represents a javascript node. - * - * @author Fabien Potencier - */ -class JavascriptNode extends \Twig_Node -{ - public function __construct(\Twig_Node_Expression $expr, \Twig_Node_Expression $attributes, $lineno, $tag = null) - { - parent::__construct(array('expr' => $expr, 'attributes' => $attributes), 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("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.javascripts')->add(") - ->subcompile($this->getNode('expr')) - ->raw(', ') - ->subcompile($this->getNode('attributes')) - ->raw(");\n") - ; - } -} \ No newline at end of file diff --git a/Twig/Node/JavascriptsNode.php b/Twig/Node/JavascriptsNode.php deleted file mode 100644 index dbbb300..0000000 --- a/Twig/Node/JavascriptsNode.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\Node; - -/** - * Represents a javascripts node. - * - * @author Fabien Potencier - */ -class JavascriptsNode extends \Twig_Node -{ - public function __construct($lineno, $tag = null) - { - parent::__construct(array(), 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("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.javascripts')->render();\n") - ; - } -} \ No newline at end of file diff --git a/Twig/Node/StylesheetNode.php b/Twig/Node/StylesheetNode.php deleted file mode 100644 index 6358ea1..0000000 --- a/Twig/Node/StylesheetNode.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\Node; - -/** - * Represents a stylesheet node. - * - * @author Fabien Potencier - */ -class StylesheetNode extends \Twig_Node -{ - public function __construct(\Twig_Node_Expression $expr, \Twig_Node_Expression $attributes, $lineno, $tag = null) - { - parent::__construct(array('expr' => $expr, 'attributes' => $attributes), 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("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.stylesheets')->add(") - ->subcompile($this->getNode('expr')) - ->raw(', ') - ->subcompile($this->getNode('attributes')) - ->raw(");\n") - ; - } -} \ No newline at end of file diff --git a/Twig/Node/StylesheetsNode.php b/Twig/Node/StylesheetsNode.php deleted file mode 100644 index 30ffbfe..0000000 --- a/Twig/Node/StylesheetsNode.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\Node; - -/** - * Represents a stylesheets node. - * - * @author Fabien Potencier - */ -class StylesheetsNode extends \Twig_Node -{ - public function __construct($lineno, $tag = null) - { - parent::__construct(array(), 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("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.stylesheets')->render();\n") - ; - } -} \ No newline at end of file diff --git a/Twig/TokenParser/JavascriptTokenParser.php b/Twig/TokenParser/JavascriptTokenParser.php deleted file mode 100644 index b217a50..0000000 --- a/Twig/TokenParser/JavascriptTokenParser.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\TokenParser; - -use BeSimple\PackingMinifyBundle\Twig\Node\JavascriptNode; - -/** - * - * - * @author Fabien Potencier - */ -class JavascriptTokenParser 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) - { - $expr = $this->parser->getExpressionParser()->parseExpression(); - - // attributes - if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) { - $this->parser->getStream()->next(); - - $attributes = $this->parser->getExpressionParser()->parseExpression(); - } else { - $attributes = new \Twig_Node_Expression_Array(array(), $token->getLine()); - } - - $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - - return new JavascriptNode($expr, $attributes, $token->getLine(), $this->getTag()); - } - - /** - * Gets the tag name associated with this token parser. - * - * @param string The tag name - */ - public function getTag() - { - return 'javascript'; - } -} \ No newline at end of file diff --git a/Twig/TokenParser/JavascriptsTokenParser.php b/Twig/TokenParser/JavascriptsTokenParser.php deleted file mode 100644 index a2bb240..0000000 --- a/Twig/TokenParser/JavascriptsTokenParser.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\TokenParser; - -use BeSimple\PackingMinifyBundle\Twig\Node\JavascriptsNode; - -/** - * - * - * @author Fabien Potencier - */ -class JavascriptsTokenParser 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) - { - $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - - return new JavascriptsNode($token->getLine(), $this->getTag()); - } - - /** - * Gets the tag name associated with this token parser. - * - * @param string The tag name - */ - public function getTag() - { - return 'javascripts'; - } -} \ No newline at end of file diff --git a/Twig/TokenParser/StylesheetTokenParser.php b/Twig/TokenParser/StylesheetTokenParser.php deleted file mode 100644 index c0533f9..0000000 --- a/Twig/TokenParser/StylesheetTokenParser.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\TokenParser; - -use BeSimple\PackingMinifyBundle\Twig\Node\StylesheetNode; - -/** - * - * - * @author Fabien Potencier - */ -class StylesheetTokenParser 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) - { - $expr = $this->parser->getExpressionParser()->parseExpression(); - - // attributes - if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) { - $this->parser->getStream()->next(); - - $attributes = $this->parser->getExpressionParser()->parseExpression(); - } else { - $attributes = new \Twig_Node_Expression_Array(array(), $token->getLine()); - } - - $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - - return new StylesheetNode($expr, $attributes, $token->getLine(), $this->getTag()); - } - - /** - * Gets the tag name associated with this token parser. - * - * @param string The tag name - */ - public function getTag() - { - return 'stylesheet'; - } -} \ No newline at end of file diff --git a/Twig/TokenParser/StylesheetsTokenParser.php b/Twig/TokenParser/StylesheetsTokenParser.php deleted file mode 100644 index c33578b..0000000 --- a/Twig/TokenParser/StylesheetsTokenParser.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace BeSimple\PackingMinifyBundle\Twig\TokenParser; - -use BeSimple\PackingMinifyBundle\Twig\Node\StylesheetsNode; - -/** - * - * - * @author Fabien Potencier - */ -class StylesheetsTokenParser 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) - { - $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - - return new StylesheetsNode($token->getLine(), $this->getTag()); - } - - /** - * Gets the tag name associated with this token parser. - * - * @param string The tag name - */ - public function getTag() - { - return 'stylesheets'; - } -} \ No newline at end of file