Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 327
sandra Avatar asked May 30 '10 14:05

sandra


People also ask

What is Django's template engine?

Django's template engine provides a powerful mini-language for defining the user-facing layer of your application, encouraging a clean separation of application and presentation logic. Templates can be maintained by anyone with an understanding of HTML; no knowledge of Python is required.

What does {% mean in Django?

{% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

How do you solve template does not exist in Django?

Django TemplateDoesNotExist error means simply that the framework can't find the template file. To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file ( settings.py ) by TEMPLATE_DIRS setting.

How do I use a downloaded Django template?

To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.


1 Answers

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

like image 134
apenwarr Avatar answered Oct 22 '22 03:10

apenwarr