| 1 |
# {% raw %} |
| 2 |
# Example customize.py file for j2cli |
| 3 |
# Contains potional hooks that modify the way j2cli is initialized |
| 4 |
|
| 5 |
|
| 6 |
def j2_environment_params(): |
| 7 |
""" Extra parameters for the Jinja2 Environment """ |
| 8 |
# Jinja2 Environment configuration |
| 9 |
# http://jinja.pocoo.org/docs/2.10/api/#jinja2.Environment |
| 10 |
return dict( |
| 11 |
# Just some examples |
| 12 |
|
| 13 |
# Change block start/end strings |
| 14 |
block_start_string='<%', |
| 15 |
block_end_string='%>', |
| 16 |
# Change variable strings |
| 17 |
variable_start_string='<<', |
| 18 |
variable_end_string='>>', |
| 19 |
# Remove whitespace around blocks |
| 20 |
trim_blocks=True, |
| 21 |
lstrip_blocks=True, |
| 22 |
# Enable line statements: |
| 23 |
# http://jinja.pocoo.org/docs/2.10/templates/#line-statements |
| 24 |
line_statement_prefix='#', |
| 25 |
# Keep \n at the end of a file |
| 26 |
keep_trailing_newline=True, |
| 27 |
# Enable custom extensions |
| 28 |
# http://jinja.pocoo.org/docs/2.10/extensions/#jinja-extensions |
| 29 |
extensions=('jinja2.ext.i18n',), |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
def j2_environment(env): |
| 34 |
""" Modify Jinja2 environment |
| 35 |
|
| 36 |
:param env: jinja2.environment.Environment |
| 37 |
:rtype: jinja2.environment.Environment |
| 38 |
""" |
| 39 |
env.globals.update( |
| 40 |
my_function=lambda v: 'my function says "{}"'.format(v) |
| 41 |
) |
| 42 |
return env |
| 43 |
|
| 44 |
|
| 45 |
def alter_context(context): |
| 46 |
""" Modify the context and return it """ |
| 47 |
# An extra variable |
| 48 |
context['ADD'] = '127' |
| 49 |
return context |
| 50 |
|
| 51 |
|
| 52 |
def extra_filters(): |
| 53 |
""" Declare some custom filters. |
| 54 |
|
| 55 |
Returns: dict(name = function) |
| 56 |
""" |
| 57 |
return dict( |
| 58 |
# Example: {{ var | parentheses }} |
| 59 |
parentheses=lambda t: '(' + t + ')', |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
def extra_tests(): |
| 64 |
""" Declare some custom tests |
| 65 |
|
| 66 |
Returns: dict(name = function) |
| 67 |
""" |
| 68 |
return dict( |
| 69 |
# Example: {% if a|int is custom_odd %}odd{% endif %} |
| 70 |
custom_odd=lambda n: True if (n % 2) else False |
| 71 |
) |
| 72 |
|
| 73 |
# {% endraw %} |