$filter = new Twig_SimpleFilter('rot13', 'str_rot13', $options);
-Environment aware Filters
+Environment-aware Filters
~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to access the current environment instance in your filter, set the
return str_rot13($string);
}, array('needs_environment' => true));
-Context aware Filters
+Context-aware Filters
~~~~~~~~~~~~~~~~~~~~~
If you want to access the current context in your filter, set the
}
}
-The above example, shows how you can create tests that use a node class. The
+The above example shows how you can create tests that use a node class. The
node class has access to one sub-node called 'node'. This sub-node contains the
-value that is being tested. When the ``odd`` filter is used in code like:
+value that is being tested. When the ``odd`` filter is used in code such as:
.. code-block:: jinja
{% if my_value is odd %}
-The ``node`` sub-node will contain an expression of ``my_value``. Node based
+The ``node`` sub-node will contain an expression of ``my_value``. Node-based
tests also have access to the ``arguments`` node. This node will contain the
various other arguments that have been provided to your test.
Tags
----
-One of the most exciting feature of a template engine like Twig is the
+One of the most exciting features of a template engine like Twig is the
possibility to define new language constructs. This is also the most complex
feature as you need to understand how Twig's internals work.
Operators
~~~~~~~~~
-The ``getOperators()`` methods allows to add new operators. Here is how to add
+The ``getOperators()`` methods lets you add new operators. Here is how to add
``!``, ``||``, and ``&&`` operators::
class Project_Twig_Extension extends Twig_Extension
Tests
~~~~~
-The ``getTests()`` methods allows to add new test functions::
+The ``getTests()`` method lets you add new test functions::
class Project_Twig_Extension extends Twig_Extension
{
Twig is very extensible and you can easily hack it. Keep in mind that you
should probably try to create an extension before hacking the core, as most
-features and enhancements can be done with extensions. This chapter is also
+features and enhancements can be handled with extensions. This chapter is also
useful for people who want to understand how Twig works under the hood.
-How Twig works?
+How does Twig work?
---------------
The rendering of a Twig template can be summarized into four key steps:
for easier processing;
* Then, the **parser** converts the token stream into a meaningful tree
of nodes (the Abstract Syntax Tree);
- * Eventually, the *compiler* transforms the AST into PHP code;
+ * Eventually, the *compiler* transforms the AST into PHP code.
* **Evaluate** the template: It basically means calling the ``display()``
method of the compiled template and passing it the context.