Posts Tagged ‘template engine

04
Oct
09

PyML(Python Markup Language) template engine

I know it’s been a while since my last post. I’ve been busy with setting up and pulling down pepijndevos.nl(after I decided to go for another blog system), I’ve spend a good deal of time working on PyMouse and worked on some projects for myself and for clients.

I want to share one of those projects with you. I decided to make a CMS like Jekyll and Hyde(not the book!), since I liked neither of the previous systems. So like a lot of people who like reinventing wheels I started to look for a nice markup language for the user and a nice template language for the designer.

I choose for Markdown right away, end of story here…

Because of the little experience I have with Pylons I looked ad Genshi and Mako initially(can’t help the Chinese feeling these names give me). Personally the ugly tags in most template languages make me run away, so I threw Make out of the window, together with all the other ugly-tag-based languages.

I like the idea of working with valid XML attributes for templating, so I started of with Genshi initially, especially since speed isn’t a major point for a static blog!

Days later I came across this wiki page on the Python website: http://wiki.python.org/moin/Templating

Looking at Dirty I remembered my own attempts to write a template language implemented as Python objects(extending dict, for free functionality).

The problem with writing such a thing is described in the code below, I mailed the developer of Dirty about how he solved the problem — he didn’t.

>>> def div(*content, **attributes):
...     pass
...
>>> div("hello world!", id="test") # Doesn't html have the attributes first!? Ugly!
>>> def div(**attributes, *content): # Not allowed!
  File "", line 1
    def div(**attributes, *content):
                        ^
SyntaxError: invalid syntax
>>> def div(atributes, *content):
...     pass
...
>>> div({'id':'test'}, "hello world!") # Even uglier!

But then I came up with this slightly brilliant solution:

When you define __call__ on a class it allows you to call the class, no big deal, eh? Wrong! It allows you to write this:

html(xmlns="http://www.w3.org/1999/xhtml")(
    head( # No atributes
        title("Hello world")
    ),
    body(
        p(id="test")(
            "Hi", br(),
            "How are you?"
        ),
        img(src="test.img", alt="just a test") # No content
        ul(
            *[li(c) for c in xrange(10)] # Now that is cool!
        )
    )
)

In short PyML is a pure Python combination of a string and a dict looking like html when written and printed!

You can do all sorts of things with this that you normally do with dictionaries, strings and lists, like sorting and filtering them, change a p into a div or getting/setting attributes later with square brackets. You could define functions to return a snippet or you could include templates into others. If you’re finished with them, just print the object!

Oh, about the speed? Quite good for my hobby project! (Unreliable micro benchmark ahead!)

Render time Engine
0.03160 PyML
0.50618 Genshi
0.01813 Mako

You can get it here if you want! Or wait for my complete CMS of course…

Advertisements



My blog has moved!

My blog has permanently moved to a self hosted Wordpress at http://pepijndevos.nl

This blog will stay around for accidental search engine visitors.

Me

This is me

Blog Stats

  • 22,541 hits

@PepijnDeVos

Error: Twitter did not respond. Please wait a few minutes and refresh this page.