From fc6819cec28f68b72313714453e3f25eb2323a87 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 10 Sep 2011 11:35:39 +0200 Subject: [PATCH] added support for twig C extension --- CHANGELOG | 1 + doc/intro.rst | 32 ++++++++++++++++++++--- lib/Twig/Node/Expression/GetAttr.php | 6 +++- test/Twig/Tests/Node/Expression/GetAttrTest.php | 6 ++-- test/Twig/Tests/Node/TestCase.php | 9 ++++++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 421fa70..158cfdc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.4.0-RC2 + * added the Twig C extension (replaces the implementation of Twig_Template::getAttribute() by a C function) * added negative timestamp support to the date filter * 1.4.0-RC1 (2011-11-20) diff --git a/doc/intro.rst b/doc/intro.rst index 9cec815..951ec91 100644 --- a/doc/intro.rst +++ b/doc/intro.rst @@ -32,8 +32,8 @@ Installation You have multiple ways to install Twig. If you are unsure what to do, go with the tarball. -From the tarball release -~~~~~~~~~~~~~~~~~~~~~~~~ +Installing grom the tarball release +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Download the most recent tarball from the `download page`_ 2. Unpack the tarball @@ -43,8 +43,8 @@ Installing the development version ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Install Subversion or Git -2. For Subversion: ``svn co http://svn.twig-project.org/trunk/ twig``, for Git: - ``git clone git://github.com/fabpot/Twig.git`` +2. For Git: ``git clone git://github.com/fabpot/Twig.git`` +3. For Subversion: ``svn co http://svn.twig-project.org/trunk/ twig`` Installing the PEAR package ~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -53,6 +53,30 @@ Installing the PEAR package 2. ``pear channel-discover pear.twig-project.org`` 3. ``pear install twig/Twig`` (or ``pear install twig/Twig-beta``) +Installing the C extension +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 1.4 + The C extension was added in Twig 1.4. + +Twig comes with a C extension that enhances the performance of the Twig +runtime engine. You can install it like any other PHP extension: + +.. code-block:: bash + + $ cd ext/twig + $ phpize + $ ./configure + $ make + $ make install + +Finally, enable the extension in your ``php.ini`` configuration file: + + extension=twig.so + +And from now on, Twig will automatically compiles your templates to take +advantage of the C extension. + Basic API Usage --------------- diff --git a/lib/Twig/Node/Expression/GetAttr.php b/lib/Twig/Node/Expression/GetAttr.php index 8d7184b..e6f5ba2 100644 --- a/lib/Twig/Node/Expression/GetAttr.php +++ b/lib/Twig/Node/Expression/GetAttr.php @@ -18,7 +18,11 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { - $compiler->raw('$this->getAttribute('); + if (function_exists('twig_template_get_attributes')) { + $compiler->raw('twig_template_get_attributes($this, '); + } else { + $compiler->raw('$this->getAttribute('); + } if ($this->getAttribute('ignore_strict_check')) { $this->getNode('node')->setAttribute('ignore_strict_check', true); diff --git a/test/Twig/Tests/Node/Expression/GetAttrTest.php b/test/Twig/Tests/Node/Expression/GetAttrTest.php index 264618d..3893748 100644 --- a/test/Twig/Tests/Node/Expression/GetAttrTest.php +++ b/test/Twig/Tests/Node/Expression/GetAttrTest.php @@ -49,10 +49,10 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase $attr = new Twig_Node_Expression_Constant('bar', 0); $args = new Twig_Node(); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 0); - $tests[] = array($node, sprintf('$this->getAttribute(%s, "bar")', $this->getVariableGetter('foo'))); + $tests[] = array($node, sprintf('%s%s, "bar")', $this->getAttributeGetter(), $this->getVariableGetter('foo'))); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0); - $tests[] = array($node, sprintf('$this->getAttribute(%s, "bar", array(), "array")', $this->getVariableGetter('foo'))); + $tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo'))); $args = new Twig_Node(array( @@ -60,7 +60,7 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase new Twig_Node_Expression_Constant('bar', 0), )); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 0); - $tests[] = array($node, sprintf('$this->getAttribute(%s, "bar", array(%s, "bar", ), "method")', $this->getVariableGetter('foo'), $this->getVariableGetter('foo'))); + $tests[] = array($node, sprintf('%s%s, "bar", array(%s, "bar", ), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo'), $this->getVariableGetter('foo'))); return $tests; } diff --git a/test/Twig/Tests/Node/TestCase.php b/test/Twig/Tests/Node/TestCase.php index ec4eee4..f142529 100644 --- a/test/Twig/Tests/Node/TestCase.php +++ b/test/Twig/Tests/Node/TestCase.php @@ -46,4 +46,13 @@ abstract class Twig_Tests_Node_TestCase extends PHPUnit_Framework_TestCase return sprintf('$this->getContext($context, "%s")', $name); } + + protected function getAttributeGetter() + { + if (function_exists('twig_template_get_attributes')) { + return 'twig_template_get_attributes($this, '; + } + + return '$this->getAttribute('; + } } -- 1.7.2.5