views:

257

answers:

3

Hi folks,

I recently wrote a simple and tiny embedded HTTP server for my C++ app (QT) and I played a little bit with Ry's http-parser and loved it. This guy is crazy.

So I told to myself: "Hey! Why not port the django template engine to C?" That'd be awesome! I know, it won't be an easy task (not at all, I know) but I'd really love to implement this. So I came here for inspiration, ideas, opinions...

I'd really love to have some pointers on the subject, ideas, what is already done, which major problems I'll encounter (and how to solve them) - How not to reinvent the wheel... anyway, you got the idea :)

Thanks a million times!

P.S. Simple code snippets, and links to tools and libs are very welcome!

P.P.S. I'm already aware of grantlee, I took a look into its sources. Well... that's C++ and it's specific to Qt.

+2  A: 

Hmm, I don't see why anything in the django templates code would be hard to implement in C. The template syntax looks a bit pythonic, but it's not actually python; they implemented their own parser for it. So the first place to look would be the django template implementation in python. It's really not much code, and it's reasonably easy to understand.

Of course, C will be much more verbose. What you're writing is exactly a compiler: it reads in some code (the django templates) and writes out some code in another language (the html). So all the documentation you can find about writing compilers in C (ie. tokenizers + parsers + code generators) is relevant.

First you'll probably want to generate a syntax tree. The syntax of django templates is very regular... every {% whatever %} block has a corresponding {% endwhatever %} block, so your parser could actually generate the tree without actually knowing what all the whatever keywords might be.

Then, you walk through the tree, doing the "code gen" phase for each block. For example, you'd codegen {% if %} by checking the value of the if parameter, and printing either its contents or nothing, depending whether the if clause is true or false. And so on with while loops, filters, blocks, etc.

Mind you, all this is a lot of work... have you considered just embedding a python interpreter into your C program? (Seriously! It's not that hard to do, since the python interpreter is open source.)

apenwarr
Thank you, apenwarr! That's in fact what I'm doing right now. Writing a tokenizer first and then a parser for every token. and assemble the whole thing. That won't be easy and I have to find a nice way to declare and use Contexts of variables of different types. But that's fun anyway :)
sandra
A: 

Are you still working on this? I'd be interested in seeing what you came up with and using it as a starting point pretty soon. I've got a couple things in use right now, like a couple simple POSIX regexs in ctypes to do the most common operations for me. It's proven to be faster when looping large amounts of replacements without leaving cpp.

Joshua Jonah
A: 

If you or anyone else is interested in a C++ implementation of the Django template engine, I am working on it: http://git.fawek.net/Cjango/. It's not ready yet though.

Jakub Wieczorek
How does it handle custom template tags and filters?
Ignacio Vazquez-Abrams
TagNodeFactory and FilterFactory are the two interfaces that the client side would use to inject custom functionality. The former takes a factory function (for each tag name) that allocates an object of a TagNode subclass every time it is needed in the template tree. With the latter you just assign a Filter object to a given filter name.
Jakub Wieczorek