TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Building a toy template engine in Python

76 pointsby alexmicabout 12 years ago

10 comments

js2about 12 years ago
A nice complement to this article is <a href="http://effbot.org/zone/simple-top-down-parsing.htm" rel="nofollow">http://effbot.org/zone/simple-top-down-parsing.htm</a> and its earlier piece <a href="http://effbot.org/zone/simple-iterator-parser.htm" rel="nofollow">http://effbot.org/zone/simple-iterator-parser.htm</a><p>Also, this page maintains links to many python parsing options - <a href="http://nedbatchelder.com/text/python-parsers.html" rel="nofollow">http://nedbatchelder.com/text/python-parsers.html</a>
thisishugoabout 12 years ago
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.<p>Edit: which isn't meant to detract from the fact that this is a great article.<p>[1]<a href="http://www.youtube.com/watch?v=HxaD_trXwRE" rel="nofollow">http://www.youtube.com/watch?v=HxaD_trXwRE</a>
评论 #5537439 未加载
paradox95about 12 years ago
Try Pyxl<p><a href="https://github.com/dropbox/pyxl" rel="nofollow">https://github.com/dropbox/pyxl</a><p>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.<p>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.
peter_l_downsabout 12 years ago
Great 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.
jamesconroyfinnabout 12 years ago
I was particularly interested in building parsers a short while back, and your post rekindled my interest.<p>So, in the interest of sharing I thought I'd mention a couple of interesting libraries I came across.<p>In Ruby I think Temple (<a href="https://github.com/judofyr/temple" rel="nofollow">https://github.com/judofyr/temple</a>) is really quite cool. It's designed to make creating templating languages really easy, and constructs ASTs using user-defined parsers.<p>And even more interesting in my opinion is Haskell's Parsec (<a href="http://www.haskell.org/haskellwiki/Parsec" rel="nofollow">http://www.haskell.org/haskellwiki/Parsec</a>). It uses combinators to build up parsers, and can produce friendly parse error messages "for free."<p>Thanks for the intersting read.
thomasleeabout 12 years ago
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. :)<p>Something along these lines: <a href="https://github.com/thomaslee/viking-poc/blob/master/viking#L334" rel="nofollow">https://github.com/thomaslee/viking-poc/blob/master/viking#L...</a><p>(Disclaimer: I wrote the Python-AST-to-PyObject transformation code for Python 2.6 that made this sort of stuff possible -- fun hack!)
tomasandrleabout 12 years ago
Interesting! I just finished writing a very similar engine in C++. <a href="https://github.com/catnapgames/NLTemplate" rel="nofollow">https://github.com/catnapgames/NLTemplate</a>
marinosabout 12 years ago
thanks for sharing this alexmic. I tried to built my own template engine but I think your approach is much better
评论 #5535392 未加载
fredsters_sabout 12 years ago
really nice conceptual approach.
CMGSabout 12 years ago
jinja2 slower than django?