Jinja2 Coding Style / Best Practices

Do you have a best practice and coding style when developing with Jinja2?

Personally, I like the style in Plurk / Solace , but I would like to know what other styles and practices people use when writing Jinja2.

+6
jinja2
source share
2 answers

Chromium has a detailed Jinja style guide - I am the author of the original, based on personal use, peer reviews and viewing others, code.

Aside from Jinja’s special recommendations - basically, β€œkeep it simple because it’s an unfamiliar DSL” and many tips - the subtlest question is how to structure your Python code and Python / Jinja interaction. Our main findings:

  • Logic in Python (Python should go above a single line, keeping Jinja simple).
  • One way flow: Python -> Jinja. Do not call Python from Jinja (other than custom filters) to avoid complexity.
  • Define each context in one dictionary . This is your Python / Jinja interface, and is much easier to understand than building a dictionary in stages.

Jinja has powerful features, but most use fairly simple templates written by people who rarely use Jinja, so the goal is to put text snippets and basic string processing in Jinja, but preserve complex logic in Python, which is better-prepared and more familiar.

+3
source share

As a set of examples of Jinja2 styles, here is a list of his projects:

+3
source share

All Articles