5 ms·
Building a toy template engine in Python
- fredsters_s 13y agoreally nice conceptual approach.
- marinos 13y agothanks for sharing this alexmic. I tried to built my own template engine but I think your approach is much better
- alexmic 13y agoThanks – what was your approach?
- js2 13y agoA nice complement to this article is http://effbot.org/zone/simple-top-down-parsing.htm http://effbot.org/zone/simple-top-down-parsing.htm and its earlier piece http://effbot.org/zone/simple-iterator-parser.htm http://effbot.org/zone/simple-iterator-parser.htm Also, this page maintains links to many python parsing options - http://nedbatchelder.com/text/python-parsers.html http://nedbatchelder.com/text/python-parsers.html
- peter_l_downs 13y agoGreat blog post! I did something very similar when I was working on an implementation of the Mustache templating language. If you haven't built a simple recursive descent parser already, I highly recommend doing so — it helped me get a lot better at programming.
- paradox95 13y agoTry Pyxl https://github.com/dropbox/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.
- thomaslee 13y agoInteresting 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#L334 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!)
- thisishugo 13y agoI'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 http://www.youtube.com/watch?v=HxaD_trXwRE
- alexmic 13y agoThanks for the video – this was my first stub at template engines so I'm all for learning about better approaches.
- jamesconroyfinn 13y agoI was particularly interested in building parsers a short while back, and your post rekindled my interest. So, in the interest of sharing I thought I'd mention a couple of interesting libraries I came across. In Ruby I think Temple (https://github.com/judofyr/temple https://github.com/judofyr/temple) is really quite cool. It's designed to make creating templating languages really easy, and constructs ASTs using user-defined parsers. And even more interesting in my opinion is Haskell's Parsec (http://www.haskell.org/haskellwiki/Parsec http://www.haskell.org/haskellwiki/Parsec). It uses combinators to build up parsers, and can produce friendly parse error messages "for free." Thanks for the intersting read.
- tomasandrle 13y agoInteresting! I just finished writing a very similar engine in C++. https://github.com/catnapgames/NLTemplate https://github.com/catnapgames/NLTemplate
- CMGS 13y agojinja2 slower than django?