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 ;-)
Update: Please read the new blogpost which reveals the new location and maintenance of the code.
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.
— Toothy on Saturday, February 16, 2008 1:15 #
great work - its much more pleasant than html/xhtml and gets both correct in the output :)
— Ronny Pfannschmidt on Saturday, February 16, 2008 10:18 #
Wonderful! 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).
— Waldemar Kornewald on Saturday, February 16, 2008 11:29 #
take a look at clevercss - its in the pocoo sandbox
— Ronny Pfannschmidt on Monday, February 18, 2008 9:02 #
This looks like ruby.
A fragment for your imagination:
<pre> %li[for item in navigation] %a{'href': item.href} $item.caption </pre>
woud be:
<pre> navigation.foreach |item| ->
li.do
a {href => item.href} item.caption
end
end</pre>
— Meneer R on Monday, February 18, 2008 15:24 #
awesome! 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.
— sandro on Wednesday, March 5, 2008 6:20 #
I am looking for something like this. Where is the code? I could not find it on your site. Thanks.
— Toby Ho on Wednesday, March 19, 2008 17:46 #
The sourecode is part of the pocoo sandbox hg repository: hg clone dev.pocoo.org/hg/sandbox
— Armin Ronacher on Friday, March 21, 2008 10:27 #
This 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 allowexec=allowexec) 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.
— Bob on Friday, May 2, 2008 2:26 #
To use GHRML templates in Django, borrow from the adaptor for Mako/Genshi in Django at www.djangosnippets.org/snippets/97/ , fuzzythinker.blogspot.com/2007/04/using-mako-in-django.html and apply this patch to genshi_django.py:
-loader = TemplateLoader(templatedirs, autoreload=settings.DEBUG) +loader = TemplateLoader(templatedirs, autoreload=settings.DEBUG, default_class=GHRMLTemplate)
— rawagajah on Sunday, May 4, 2008 21:52 #
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
— rawagajah on Sunday, May 4, 2008 22:28 #
Bob [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)
— rawagajah on Monday, May 5, 2008 9:17 #
I 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?
— Damian on Thursday, May 8, 2008 9:52 #
Regarding 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.
— Armin Ronacher on Friday, May 9, 2008 12:42 #