kblomqvist / yasha

A command-line tool to render Jinja templates for great good
MIT License
243 stars 23 forks source link

How to override Jinja2 block_start_string #43

Closed elgalu closed 6 years ago

elgalu commented 6 years ago

Hi, I'm trying to override certain Jinja2 environment defaults like block_start_string:

import jinja2

jinja_env = jinja2.Environment(
    block_start_string = '<%',
    block_end_string = '%>',
    variable_start_string = '<<',
    variable_end_string = '>>'
)

I'm loading that extension file with yasha -e my_extension.py however it doesn't take effect as I don't know how to activate that jinja_env I'm creating.

Thanks for maintaining this project!

kblomqvist commented 6 years ago

Unfortunately that's not possible. Yasha's file extensions support only classes derived from jinja2.ext.Extension, not jinja2.Environment. However, I would like to support Environments too as I have had plans to use Yasha for Latex, see #31 and #33.

elgalu commented 6 years ago

Thanks for the prompt response @kblomqvist and very glad to read its on your plans:)

elgalu commented 6 years ago

Can you point me where in there source could I hard-code this settings? until is done "properly" ?

kblomqvist commented 6 years ago

https://github.com/kblomqvist/yasha/blob/master/yasha/yasha.py#L127

kblomqvist commented 6 years ago

A note to myself. Jinja's defaults could be overridden by local keys defined in Yasha's file extensions.

# extension.py
BLOCK_START_STRING = '{%'
BLOCK_END_STRING = '%}'
VARIABLE_START_STRING = '{{'
VARIABLE_END_STRING = '}}'
COMMENT_START_STRING = '{#'
COMMENT_END_STRING = '#}'
LINE_STATEMENT_PREFIX = None
LINE_COMMENT_PREFIX = None
TRIM_BLOCKS = False
LSTRIP_BLOCKS = False
NEWLINE_SEQUENCE = '\n'
KEEP_TRAILING_NEWLINE = False
kblomqvist commented 6 years ago

@elgalu Could you try if https://github.com/kblomqvist/yasha#template-syntax works for you?

elgalu commented 6 years ago

Works!! Thanks