Hacker News new | ask | show | jobs
by scardine 2492 days ago
> Also, you don't get much by switching Django templates to Jinja.

Well, you gain a lot of performance - as much as 10x depending on the complexity of your template. Also, Jinja is a lot less crippled than Django Templates (for example, you can't call a function/method with parameters in Django Templates).

2 comments

Bear in mind "crippled" was a deliberate design choice. Templates have a habit of taking over business logic and before you know it you've reinvented old school PHP.
It is a design decision from a time where the general belief was that template designers played in a lower league compared to "real" software developers.

It is 2019 and personally I see no reason to think professional template designers will shoot themselves in the foot if we give them too much power. We are all consenting adults...

A stated reason is:

https://django-best-practices.readthedocs.io/en/latest/appli...

A common pattern in MVC-style programming is to build thick/fat models and thin controllers. For Django this translates to building models with lots of small methods attached to them and views which use those methods to keep their logic as minimal as possible. There are lots of benefits to this approach.

DRY: Rather than repeating the same logic in multiple views, it is defined once on the model.

Testable: Breaking up logic into small methods on the model makes your code easier to unit test.

Sound reasons but there is nothing preventing you to adopt this pattern with Jinja. :-)

I'm glad that Jinja2 templates are now first-class citizens in Django as I care about performance and don't feel like patronizing template designers with a carefully handicapped template language.

Thank you for pointing to the performance improvement, although I never found the templates to be the bottleneck (insert usual suspects here) it's good to know. Since I actually started my journey into python web development with flask+jinja2 and while I found the lack of parameters in Django templates disturbing at first, in practice it never really stopped me (custom template tags and filters...)