* 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)
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
---------------
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);
$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(
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;
}
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(';
+ }
}