Live data from Hacker News

Building a toy template engine in Python

alexmic.net

1–10 of 13 posts

Re: Building a toy template engine in Python

#7
Try Pyxl

https://github.com/dropbox/pyxl

I don't work at Dropbox but I did work at Cove where this was originally developed and it is great. Makes working with HTML in Python so easy.

I also my own version of it that I have rewritten to treat the HTML object more like jQuery does and added more jQuery methods to it.

Re: Building a toy template engine in Python

#8
Interesting extension of this exercise would be to replace your custom AST with the AST exposed by Python, then use the compile() builtin to compile said AST into a PyCodeObject ready for direct execution by the Python VM. :)

Something along these lines: https://github.com/thomaslee/viking-poc/blob/master/viking#L...

(Disclaimer: I wrote the Python-AST-to-PyObject transformation code for Python 2.6 that made this sort of stuff possible -- fun hack!)

Re: Building a toy template engine in Python

#9
I'm not sure I like the idea of parsing templates with regular expressions. There is a better way, as demonstrated in Robe Pike's video[1] detailing how the Go template language is implemented.

Edit: which isn't meant to detract from the fact that this is a great article.

[1]http://www.youtube.com/watch?v=HxaD_trXwRE

Re: Building a toy template engine in Python

#10

I'm not sure I like the idea of parsing templates with regular expressions. There is a better way, as demonstrated in Robe Pike's video[1] detailing how the Go template language is implemented. Edit: which isn't meant to detract from the fact that this is a great article. [1] http://www.youtube.com/watch?v=HxaD_trXwRE

Thanks for the video – this was my first stub at template engines so I'm all for learning about better approaches.
Post reply on HN