Adding a custom filter to jinja2 under pyramid
jinja2, pyramid, python
Solution
Assuming you are using `pyramid_jinja2`, you can use `pyramid_jinja2.get_jinja2_environment()` via the `configurator` instance to access the environment.
However, apparently you can also register them via the pyramid config file without accessing the env directly:
[app:yourapp]
# ... other stuff ...
jinja2.filters =
# ...
getbitvalue = your_package.your_subpackage:GetBitValue
Problem
This question has been asked before but the accepted solution (given by the question poster himself) says that we can add the new filter to jinja2.filter.FILTER straightaway. But in the jinja2 documentation, it is advised that the filter be added to the environment. I am developing an app under pyramid and need to define my custom filter and do the following. ``` from jinja2 import Environment #Define a new filter def GetBitValue(num,place): y = (num >> (place-1)) & 1 return y env = Environment() env.filters['getbitvalue'] = GetBitValue ``` Where should this code fragment be placed? I tried placing it in the views file but that obviously did not work. If i place it in `__init__.py`, how do I make sure that jinja2 picks it up? I mean how do I send back the `env` to jinja2 settings under pyramid?