Extensions can be used to add extra functionality to the Jinja template system at the parser level. Custom extensions are bound to an environment but may not store environment specific data on `self`. The reason for this is that an extension can be bound to another environment (for
| 41 | |
| 42 | |
| 43 | class Extension(with_metaclass(ExtensionRegistry, object)): |
| 44 | """Extensions can be used to add extra functionality to the Jinja template |
| 45 | system at the parser level. Custom extensions are bound to an environment |
| 46 | but may not store environment specific data on `self`. The reason for |
| 47 | this is that an extension can be bound to another environment (for |
| 48 | overlays) by creating a copy and reassigning the `environment` attribute. |
| 49 | |
| 50 | As extensions are created by the environment they cannot accept any |
| 51 | arguments for configuration. One may want to work around that by using |
| 52 | a factory function, but that is not possible as extensions are identified |
| 53 | by their import name. The correct way to configure the extension is |
| 54 | storing the configuration values on the environment. Because this way the |
| 55 | environment ends up acting as central configuration storage the |
| 56 | attributes may clash which is why extensions have to ensure that the names |
| 57 | they choose for configuration are not too generic. ``prefix`` for example |
| 58 | is a terrible name, ``fragment_cache_prefix`` on the other hand is a good |
| 59 | name as includes the name of the extension (fragment cache). |
| 60 | """ |
| 61 | |
| 62 | #: if this extension parses this is the list of tags it's listening to. |
| 63 | tags = set() |
| 64 | |
| 65 | #: the priority of that extension. This is especially useful for |
| 66 | #: extensions that preprocess values. A lower value means higher |
| 67 | #: priority. |
| 68 | #: |
| 69 | #: .. versionadded:: 2.4 |
| 70 | priority = 100 |
| 71 | |
| 72 | def __init__(self, environment): |
| 73 | self.environment = environment |
| 74 | |
| 75 | def bind(self, environment): |
| 76 | """Create a copy of this extension bound to another environment.""" |
| 77 | rv = object.__new__(self.__class__) |
| 78 | rv.__dict__.update(self.__dict__) |
| 79 | rv.environment = environment |
| 80 | return rv |
| 81 | |
| 82 | def preprocess(self, source, name, filename=None): |
| 83 | """This method is called before the actual lexing and can be used to |
| 84 | preprocess the source. The `filename` is optional. The return value |
| 85 | must be the preprocessed source. |
| 86 | """ |
| 87 | return source |
| 88 | |
| 89 | def filter_stream(self, stream): |
| 90 | """It's passed a :class:`~jinja2.lexer.TokenStream` that can be used |
| 91 | to filter tokens returned. This method has to return an iterable of |
| 92 | :class:`~jinja2.lexer.Token`\\s, but it doesn't have to return a |
| 93 | :class:`~jinja2.lexer.TokenStream`. |
| 94 | |
| 95 | In the `ext` folder of the Jinja2 source distribution there is a file |
| 96 | called `inlinegettext.py` which implements a filter that utilizes this |
| 97 | method. |
| 98 | """ |
| 99 | return stream |
| 100 |
nothing calls this directly
no test coverage detected
searching dependent graphs…