R
R
raiboon2014-12-22 11:53:45
Django
raiboon, 2014-12-22 11:53:45

How to write your own templating engine in python?

I recently read a joke that every python developer starts their acquaintance with the web by writing their own templating engine and I realized that I can’t figure out how to write it.
How to parse templates correctly? Regulars? And then suffer with recursive constructions? Write your grammars in some peg? Isn't that too slow?
How to approach caching? I know that many php-templaters translate the template into php-code in /tmp, and from there they actually give it away. How does this apply to python? Or keep everything in mind? Isn't it thick?
Will I win if the template engine is written in Cython? Due to strong typing, you can get a gain in memory. Maybe something else?
And as an additional question. I'm unlikely to use it anywhere, but it would also be interesting to integrate it into Django. Something is not googled, how to replace the django template engine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey K, 2014-12-22
@mututunus

Look at the implementation in Django and Jinja.
You can use custom templates in Django via TEMPLATE_LOADERS (example https://github.com/yuchant/django-jinja2).

A
asd111, 2014-12-23
@asd111

Regulars + eval

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question