GHRML — Haml for Genshi
May I introduce: GHRML, the Genshi Human readable markup language. name not final of course, mika would kill me ^^.
What’s GHRML? First of all it’s work in progress. But beside that it’s a pretty cool clone of Haml for Python. But no, it’s not another templating language, just a different representation for genshi markup templates. This way we can reuse all the genshi features like the directives or serializers. This gives you the full capabilities of the genshi templating language in a nice alternative syntax.
So how does it look like? Here a small example:
%html
%head
%title Hello World
%style{'type': 'text/css'}
body { font-family: sans-serif; }
%script{'type': 'text/javascript', 'src': 'foo.js'}
%body
#header
%h1 Hello World
%ul.navigation
%li[for item in navigation]
%a{'href': item.href} $item.caption
#contents
Hello World!
Most of the syntax is directly stolen from Haml. The only real change is that we don’t need explicitly marked self closing elements because genshi knows that already in the serializer. Additionally you can use brackets for genshi directives like the for-loop above. And the parser works with variable indentation, if you want an indention level of four, just do so, the parser will recognize that.
Sourcecode in the sandbox for those of you who want to try it, some updates later ;-)
Dude, why are you wasting your time with this?
It is ugly, it is inefficient and generating HTML from a silly approximated tag soup that is barely different from HTML is stupid.
Comment by Toothy — Saturday, February 16th, 2008 @ 1:15 amgreat work - its much more pleasant than html/xhtml and gets both correct in the output :)
Comment by Ronny Pfannschmidt — Saturday, February 16th, 2008 @ 10:18 amWonderful! I’ve been waiting for this *so* long (you know it ;). I hope that soon we can get rid of that redundant HTML markup. Django already makes the underlying Python code more compact. This is the first step towards more compact template code (apart from generic views). Thank a lot!
The next problem areas are CSS and JS (both of which need more than just syntax changes, I’d say).
Comment by Waldemar Kornewald — Saturday, February 16th, 2008 @ 11:29 amtake a look at clevercss - its in the pocoo sandbox
Comment by Ronny Pfannschmidt — Monday, February 18th, 2008 @ 9:02 amThis looks like ruby.
A fragment for your imagination:
%li[for item in navigation] %a{'href': item.href} $item.captionwoud be:
navigation.foreach |item| -> li.do a {href => item.href} item.caption end endComment by Meneer R — Monday, February 18th, 2008 @ 3:24 pmawesome! I’ve been using haml for the past few weeks and get really disappointed when I have to revert back to xhtml. I’m really looking forward to using genshi now.
Comment by sandro — Wednesday, March 5th, 2008 @ 6:20 amI am looking for something like this. Where is the code? I could not find it on your site. Thanks.
Comment by Toby Ho — Wednesday, March 19th, 2008 @ 5:46 pmThe sourecode is part of the pocoo sandbox hg repository: hg clone http://dev.pocoo.org/hg/sandbox
Comment by Armin Ronacher — Friday, March 21st, 2008 @ 10:27 amThis is great! I’m really missing HAML in Python, and it seems like such a good fit.
However, I have a problem running your code:
Traceback (most recent call last):
File “”, line 21, in
File “C:\Code\ghrml\sandbox\ghrml\ghrml\template.py”, line 41, in __init__
allow_exec=allow_exec)
TypeError: __init__() got an unexpected keyword argument ‘allow_exec’
which version of Genshi are you using?
I’m using 0.4.4
Would appreciate any hints.
Comment by Bob — Friday, May 2nd, 2008 @ 2:26 amTo use GHRML templates in Django, borrow from the adaptor for Mako/Genshi in Django at http://www.djangosnippets.org/snippets/97/ , http://fuzzythinker.blogspot.com/2007/04/using-mako-in-django.html and apply this patch to genshi_django.py:
-loader = TemplateLoader(template_dirs, auto_reload=settings.DEBUG)
Comment by rawagajah — Sunday, May 4th, 2008 @ 9:52 pm+loader = TemplateLoader(template_dirs, auto_reload=settings.DEBUG, default_class=GHRMLTemplate)
Bob [May 2nd, 2008], I had exactly the same problem with this same version - you can simply delete this parameter off the end of the call to MarkupTemplate.__init__ and it will work with Genshi 0.4.4
Comment by rawagajah — Sunday, May 4th, 2008 @ 10:28 pmBob [May 2nd, 2008], following on from my last comment. That change does make GHRML work with Genshi 0.4.4 well enough to process some simple templates, but ghrml/test.py still fails. To make that work, you’ll need to switch to the Subversion version of Genshi (and in my case that means the allow_exec parameter should go back in, but basedir needs to be taken out)
Comment by rawagajah — Monday, May 5th, 2008 @ 9:17 amI fail to clone the pocoo sandbox repository. Hg says “abort: premature EOF reading chunk (got 195 bytes, expected 25582)”. Could you please think of an alternative way to provide the source?
Comment by Damian — Thursday, May 8th, 2008 @ 9:52 amRegarding GHRML I’ll post a blog post in the next two days about the current status. The only way currenty is the sandbox repository and you have to checkout the whole repository, not just a path of it which is not possible with mercurial currently.
Comment by Armin Ronacher — Friday, May 9th, 2008 @ 12:42 pm