Lines Matching refs:to

6     This section describes how to extend Twig as of **Twig 1.12**. If you are
16 The first section of this chapter describes how to extend Twig easily. If
17 you want to reuse your changes in different projects or if you want to
23 When extending Twig without creating an extension, Twig won't be able to
29 different possible extension points and when to use them.
33 * ``{{ }}``: used to print the result of an expression evaluation;
35 * ``{% %}``: used to execute statements.
37 To understand why Twig exposes so many extension points, let's see how to
38 implement a *Lorem ipsum* generator (it needs to know the number of words to
58 In fact, you rarely need to create tags; and that's good news because tags are
67 Again, it works, but it looks weird. A filter transforms the passed value to
68 something else but here we use the value to indicate the number of words to
69 generate (so, ``40`` is an argument of the filter, not the value we want to
79 extension point to use. And you can use it anywhere an expression is accepted:
88 to generate lorem ipsum text:
97 Keep in mind the following when you want to extend Twig:
148 The first argument passed to the ``\Twig\TwigFilter`` constructor is the name
150 to associate with it.
152 Then, add the filter to your Twig environment::
157 And here is how to use it in a template:
167 to the filter (within parentheses ``()``) as extra arguments.
176 is compiled to something like the following::
189 If you want to access the current environment instance in your filter, set the
190 ``needs_environment`` option to ``true``; Twig will pass the current
191 environment as the first argument to the filter call::
203 If you want to access the current context in your filter, set the
204 ``needs_context`` option to ``true``; Twig will pass the current context as
205 the first argument to the filter call (or the second one if
206 ``needs_environment`` is also set to ``true``)::
221 or JavaScript code), you will want the raw output to be printed. In such a
226 Some filters may need to work on input that is already escaped or safe, for
227 example when adding (safe) HTML tags to originally unsafe output. In such a
228 case, set the ``pre_escape`` option to escape the input data before it is run
240 ``is_variadic`` option to ``true``; Twig will pass the extra arguments as the
241 last argument to the filter call as an array::
247 Be warned that :ref:`named arguments <named-arguments>` passed to a variadic
273 arguments, but after the environment and the context. For instance, a call to
274 ``'foo'|a_path_b()`` will result in the following arguments to be passed to
284 to ``true``. You can also give an alternative filter that replaces the
297 Functions are defined in the exact same way as filters, but you need to create
313 to create an instance of ``\Twig\TwigTest``::
321 Tests allow you to create custom application specific logic for evaluating
339 When creating tests you can use the ``node_class`` option to provide custom test
364 node class has access to one sub-node called 'node'. This sub-node contains the
372 tests also have access to the ``arguments`` node. This node will contain the
373 various other arguments that have been provided to your test.
378 If you want to pass a variable number of positional or named arguments to the
379 test, set the ``is_variadic`` option to ``true``. Tests support dynamic
386 possibility to define new **language constructs**. This is also the most complex
387 feature as you need to understand how Twig's internals work.
395 For instance, if you want to create a tag that converts a Markdown formatted
396 text to HTML, create a ``markdown`` filter instead:
418 For instance, if you want to create a tag that logs text, create a ``log``
425 If you still want to create a tag for a new language construct, great!
445 Three steps are needed to define a new tag:
449 * Defining a Node class (responsible for converting the parsed code to PHP);
488 The ``getTag()`` method must return the tag we want to parse, here ``set``.
494 The parsing process is simplified thanks to a bunch of methods you can call
499 * ``next()``: Moves to the next token in the stream, *but returns the old one*.
507 the token is returned and the stream moves to the next token.
516 Reading the existing ``TokenParser`` classes is the best way to learn all
557 * ``addDebugInfo()``: Adds the line of the original template file related to
571 The main motivation for writing an extension is to move often used code into a
576 Most of the time, it is useful to create a single extension for your project,
577 to host all the specific tags and filters you want to add to Twig.
581 When packaging your code into an extension, Twig is smart enough to
582 recompile your templates whenever you make a change to it (when
599 …* @deprecated since 1.23 (to be removed in 2.0), implement \Twig\Extension\InitRuntimeInterface in…
604 * Returns the token parser instances to add to the existing list.
611 * Returns the node visitor instances to add to the existing list.
618 * Returns a list of filters to add to the existing list.
625 * Returns a list of tests to add to the existing list.
632 * Returns a list of functions to add to the existing list.
639 * Returns a list of operators to add to the existing list.
646 * Returns a list of global variables to add to the existing list.
650 …* @deprecated since 1.23 (to be removed in 2.0), implement \Twig\Extension\GlobalsInterface instead
659 * @deprecated since 1.26 (to be removed in 2.0), not used anymore internally
677 Prior to Twig 1.26, you must implement the ``getName()`` method which must
681 extensions must be registered explicitly to be available in your templates.
732 To add a filter to an extension, you need to override the ``getFilters()``
733 method. This method must return an array of filters to add to the Twig
752 ``getTokenParsers()`` method. This method must return an array of tags to add
753 to the Twig environment::
767 responsible for parsing the tag and compiling it to PHP.
772 The ``getOperators()`` methods lets you add new operators. Here is how to add
816 * **functions/static methods**: Simple to implement and fast (used by all Twig
817 core extensions); but it is hard for the runtime to depend on external
820 * **closures**: Simple to implement;
825 The simplest way to use methods is to define them on the extension itself::
851 as a dependency that connects to a database engine).
855 instance on the environment that knows how to instantiate such runtime classes
862 // implement the logic to create an instance of $class
880 It is now possible to move the runtime logic to a new