Lines Matching refs:a

7     1.12**. If you are using a newer version, read the :doc:`newer<advanced>`
39 implement a *Lorem ipsum* generator (it needs to know the number of words to
42 You can use a ``lipsum`` *tag*:
48 That works, but using a tag for ``lipsum`` is not a good idea for at least
51 * ``lipsum`` is not a language construct;
62 Now, let's use a ``lipsum`` *filter*:
73 Next, let's use a ``lipsum`` *function*:
79 Here we go. For this specific example, the creation of a function is the
88 Last but not the least, you can also use a *global* object with a method able
95 As a rule of thumb, use functions for frequently used features and global
121 You can then use the ``text`` variable anywhere in a template:
130 A filter is a regular PHP function or an object method that takes the left
134 Defining a filter is as easy as associating the filter name with a PHP
135 callable. For instance, let's say you have the following code in a template:
142 associated with the ``lower`` filter. The ``lower`` filter is a built-in Twig
150 As you can see, the ``'TWIG'`` string is passed as a first argument to the PHP
166 Let's see how to create a new filter.
168 In this section, we will create a ``rot13`` filter, which should return the
169 `rot13`_ transformation of a string. Here is an example of its usage and the
178 Adding a filter is as simple as calling the ``addFilter()`` method on the
185 Here, we use ``Twig_Filter_Function`` as the filter is a PHP function. The
187 of the PHP function to call, here ``str_rot13``, a native PHP function.
189 Let's say I now want to be able to add a prefix before the converted string:
198 create a new PHP function::
212 For better encapsulation, a filter can also be defined as a static method of a
220 In an extension, you can also define a filter as a static method of the
248 or JavaScript code), you will want the raw output to be printed. In such a
254 example when adding (safe) HTML tags to originally unsafe output. In such a
266 A filter name containing the special ``*`` character is a dynamic filter as
291 arguments. For instance, a call to ``'foo'|a_path_b()`` will result in the
292 following PHP call: ``twig_path('a', 'b', 'foo')``.
297 A function is a regular PHP function or an object method that can be called from
305 associated with the ``constant`` function. The ``constant`` function is a built-in Twig
313 Adding a function is similar to adding a filter. This can be done by calling the
333 A function name containing the special ``*`` character is a dynamic function
358 arguments. For instance, a call to ``a_path_b('foo')`` will result in the
359 following PHP call: ``twig_path('a', 'b', 'foo')``.
364 One of the most exciting feature of a template engine like Twig is the
368 Let's create a simple ``set`` tag that allows the definition of simple
369 variables from within a template. The tag can be used like follows:
386 Three steps are needed to define a new tag:
388 * Defining a Token Parser class (responsible for parsing the template code);
390 * Defining a Node class (responsible for converting the parsed code to PHP);
394 Registering a new tag
397 Adding a tag is as simple as calling the ``addTokenParser`` method on the
403 Defining a Token Parser
430 The ``parse()`` method is invoked whenever the parser encounters a ``set``
431 tag. It should return a ``\Twig\Node\Node`` instance that represents the node (the
434 The parsing process is simplified thanks to a bunch of methods you can call
442 the current token is of a particular type or value (or both). The value may be an
446 type/value a syntax error is thrown. Otherwise, if the type and value are correct,
449 * ``look()``: Looks a the next token without consuming it.
459 Defining a Node
482 The compiler implements a fluid interface and provides methods that helps the
485 * ``subcompile()``: Compiles a node.
492 * ``string()``: Writes a quoted string.
494 * ``repr()``: Writes a PHP representation of a given value (see
495 ``\Twig\Node\ForNode`` for a usage example).
498 the current node as a comment.
500 * ``indent()``: Indents the generated code (see ``\Twig\Node\BlockNode`` for a
503 * ``outdent()``: Outdents the generated code (see ``\Twig\Node\BlockNode`` for a
511 The main motivation for writing an extension is to move often used code into a
516 Creating an extension also makes for a better separation of code that is
520 Most of the time, it is useful to create a single extension for your project,
526 recompile your templates whenever you make a change to it (when the
531 Before writing your own extensions, have a look at the Twig official
534 An extension is a class that implements the following interface::
560 * Returns a list of filters to add to the existing list.
567 * Returns a list of tests to add to the existing list.
574 * Returns a list of functions to add to the existing list.
581 * Returns a list of operators to add to the existing list.
588 * Returns a list of global variables to add to the existing list.
607 The ``getName()`` method must return a unique identifier for your extension.
680 To add a filter to an extension, you need to override the ``getFilters()``
706 when defining a filter to use a method::
729 Using methods for filters is a great way to package your filter without
731 at the cost of a small overhead.
761 Here, we override the ``date`` filter with a custom one. Using this extension
771 Adding a tag in an extension can be done by overriding the
785 In the above code, we have added a single new tag, defined by the