Is it possible to change the default double curly braces delimiter in polymer?
Asked Answered
A

1

13

I'd like to use polymer in a Python/Flask project. Flask uses the Jinja2 templating engine, which also uses the double curly braces {{}} as delimiters.

Escaping braces in Jinja2 is possible, but it results in very ugly and unreadable code I'd like to avoid.

Although it's also possible to change the delimiters in Jinja2, I'd rather change them in Polymer, because we already use some Jinja2 templates in the project.

So the question is, is it possible to change the delimiters in Polymer, and if so, how do we go about that?

Thanks

Alasteir answered 18/11, 2015 at 8:32 Comment(5)
Sounds a little XY - do you really need to mix two templating engines? The Polymer components don't need to be passed through a back-end parser - you should ensure they are kept separate from whatever is generating "views" on the server.Brightness
Hi @Emissary, Jinja2 is server side and Polymer client side, I can't see us building it all in just one of the two, but maybe I'm misunderstanding; are you suggesting that we keep all Polymer data binding inside a .js file for example, so that the HTML templates rendered by Jinja2 are clean of Polymer {{}} expressions?Alasteir
I'm not saying don't use both - I'm suggesting you don't mix them. The front-end specific stuff doesn't need to pass through Jinja2.Brightness
understood; so you are saying it is not necessary to have polymer templating in HTML pages directly, so passing the HTML through Jinja2 will not mess things up. Can you elaborate where the Polymer templates (should) go in such a scenario?Alasteir
I don't use Jinja2 so I've no idea how a typical project looks and I can't tell you how to structure your app, that's subjective. Web-components are encapsulated by design - you can serve all the modules directly with no server-side intervention. The only "html" file you care about is the index or end-point where your view is being dynamically generated - which can inject whatever parameters you need as attributes of the component tags specified in the markup. Components should expect data X and Y as "public" properties, rather than trying to merge data directly into their definition.Brightness
F
20

In the templates module, when initializing the jinja2 environment you can pass optional start/stop delimiters:

environment = jinja2.Environment(loader=loader, trim_blocks=True,block_start_string='@@',block_end_string='@@',variable_start_string='@=', variable_end_string='=@')

would change {% statement %} to @@ statement @@ and {{ var }} into @= var =@.

Make it a config option and only pass if set, otherwise jinja will use its own defaults.

credits: Brian Coca Link

Franks answered 3/1, 2019 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.