Skip to content

Loading…

Preprocess templates #629

Open
syrusakbary opened this Issue · 3 comments

2 participants

@syrusakbary

Hello,

I want to apply a function to the template source before is parsed by the Tornado Template engine.

Like Jinja2 Preprocessor method http://jinja.pocoo.org/docs/api/#low-level-api
Can I do something like this without touching the template loader?

If not, should be good add this option the templates (preprocess = [method1,method2,..])

Thanks!

@bdarnell
tornadoweb member

Why do you dismiss the idea of touching the template loader? That seems like the natural place to put such a feature (where would this "preprocess" directive go otherwise?). We'd just need to refactor the BaseLoader interface so there's a subclass-overridable method between the reading of the raw template data and the creation of the template (or maybe just allow the template class to be passed in instead of always using Template).

@syrusakbary

Touching the template loader is not the best way for apply preprocess to templates, because of the preprocessing method shouldnt depend on the loader used.
Jinja2 template system also integrates the preprocess method inside the Template Class https://github.com/mitsuhiko/jinja2/blob/master/jinja2/environment.py#L417

@bdarnell
tornadoweb member

The template loader is the only practical place for preprocessing to occur since it's the part that is most configurable by the application. With a little refactoring of the loader interface it would be possible to make one template loader that wraps another so you could mix preprocessing with different types of underlying loaders.

@bdarnell bdarnell added the template label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.