tera icon indicating copy to clipboard operation
tera copied to clipboard

Extensions?

Open jonathan-s opened this issue 5 years ago • 4 comments

Jinja2 has the concept of extensions that you can define yourself (templatetags in django). It would be interesting to get that feature into tera as well.

Semi-related; I was exploring the viability of building a python library on top of tera using pyo3. So in a way that would go full circle ;). One of the things missing is extensions for that.

jonathan-s avatar Dec 28 '20 19:12 jonathan-s

Tera already kind of supports that since you can add custom filters/tests/global functions. You cannot extend the parser though.

I was exploring the viability of building a python library on top of tera using pyo3.

I thought about it but you would lose some things that are too "nice" in python, like passing SQLAlchemy objects to the template (those cannot be converted to JSON for example)

Keats avatar Dec 29 '20 10:12 Keats

I thought about it but you would lose some things that are too "nice" in python, like passing SQLAlchemy objects to the template (those cannot be converted to JSON for example)

I can see that this could be an obstacle. There are object serializers though such as what the drf library provides, which could provide a bridge for this.

You cannot extend the parser though.

I understand that you cannot extend the parser to parse new elements such as {% mynewthing param param %}{% endmynewthing %}. But would it be possible to create a catch all element in the parser where mynewthing is passed onwards to lookup how that should be executed / handled?

jonathan-s avatar Dec 29 '20 10:12 jonathan-s

Maybe in a v2 but not in the current parser. Filter sections kinda cover that kind of usecases imo.

Keats avatar Dec 29 '20 12:12 Keats

I'll keep my eyes out for it :)

jonathan-s avatar Dec 29 '20 12:12 jonathan-s