(the old behavior is still possible by adding the "only" keyword)
* Moved Exceptions to Twig_Error_* (Twig_SyntaxError/Twig_RuntimeError are now Twig_Error_Syntax/Twig_Error_Runtime)
+ * added the spaceless tag
* removed trim_blocks option
* added support for is*() methods for attributes (foo.bar now looks for foo->getBar() or foo->isBar())
* changed all exceptions to extend Twig_Error
Whitespace is not further modified by the template engine, so each whitespace
(spaces, tabs, newlines etc.) is returned unchanged.
+Use the `spaceless` tag to remove whitespace between HTML tags:
+
+ [twig]
+ {% spaceless %}
+ <div>
+ <strong>foo</strong>
+ </div>
+ {% endspaceless %}
+
+ {# output will be <div><strong>foo</strong></div> #}
+
Escaping
--------
new Twig_TokenParser_Macro(),
new Twig_TokenParser_Import(),
new Twig_TokenParser_Set(),
+ new Twig_TokenParser_Spaceless(),
);
}
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Represents a spaceless node.
+ *
+ * It removes spaces between HTML tags.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class Twig_Node_Spaceless extends Twig_Node
+{
+ public function __construct(Twig_NodeInterface $body, $lineno, $tag = 'spaceless')
+ {
+ parent::__construct(array('body' => $body), array(), $lineno, $tag);
+ }
+
+ /**
+ * Compiles the node to PHP.
+ *
+ * @param Twig_Compiler A Twig_Compiler instance
+ */
+ public function compile($compiler)
+ {
+ $compiler
+ ->addDebugInfo($this)
+ ->write("ob_start();\n")
+ ->subcompile($this->getNode('body'))
+ ->write("echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n")
+ ;
+ }
+}
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_TokenParser_Spaceless 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();
+
+ $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
+ $body = $this->parser->subparse(array($this, 'decideSpacelessEnd'), true);
+ $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
+
+ return new Twig_Node_Spaceless($body, $lineno, $this->getTag());
+ }
+
+ public function decideSpacelessEnd($token)
+ {
+ return $token->test('endspaceless');
+ }
+
+ /**
+ * Gets the tag name associated with this token parser.
+ *
+ * @param string The tag name
+ */
+ public function getTag()
+ {
+ return 'spaceless';
+ }
+}
--- /dev/null
+--TEST--
+"spaceless" tag removes whites between HTML tags
+--TEMPLATE--
+{% spaceless %}
+
+ <div> <div> foo </div> </div>
+
+{% endspaceless %}
+--DATA--
+return array()
+--EXPECT--
+<div><div> foo </div></div>
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require_once dirname(__FILE__).'/TestCase.php';
+
+class Twig_Tests_Node_SpacelessTest extends Twig_Tests_Node_TestCase
+{
+ /**
+ * @covers Twig_Node_Spaceless::__construct
+ */
+ public function testConstructor()
+ {
+ $body = new Twig_Node(array(new Twig_Node_Text('<div> <div> foo </div> </div>', 0)));
+ $node = new Twig_Node_Spaceless($body, 0);
+
+ $this->assertEquals($body, $node->getNode('body'));
+ }
+
+ /**
+ * @covers Twig_Node_Spaceless::compile
+ * @dataProvider getTests
+ */
+ public function testCompile($node, $source, $environment = null)
+ {
+ parent::testCompile($node, $source, $environment);
+ }
+
+ public function getTests()
+ {
+ $body = new Twig_Node(array(new Twig_Node_Text('<div> <div> foo </div> </div>', 0)));
+ $node = new Twig_Node_Spaceless($body, 0);
+
+ return array(
+ array($node, <<<EOF
+ob_start();
+echo "<div> <div> foo </div> </div>";
+echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));
+EOF
+ ),
+ );
+ }
+}