开发者

Porting Django's templates engine to C

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.


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.)


In case it is useful, the Synth framework features a complete stand-alone re-implementation of Django's template system in C++. It includes all built-in tags and filters as well as support for custom ones; besides the library itself, there are Python bindings, in addition to a native command-line tool, which can read in data from arbitrary sources like JSON, INIs or XML.


For reference, onion has a Django-link template system.

It compiles the template to C, and even allows a little bit of gdb debugging on them.

The code generated is quite onion oriented, but with some work it can be more generic.


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.


hello why don't you use the official python binding for c++

you will be able to call python module and python script throw you c++ application

if you want to use this just include <python.h> in your application and link with python32.DLL

when you install python on your computer python come with his c++ binding by default look at this image for more details

where you can find lib include and dll

read the doc for more details or contact me

use python in c++

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜