72

I happened to stumble across HAML, an interesting and beautiful way to mark up contents and write templates for HTML.

Since I use Python and Django for my web developing need, I would like to see if there is a Python implementation of HAML (or some similar concepts -- need not be exactly identical) that can be used to replace the Django template engine.

Ber
  • 40,356
  • 16
  • 72
  • 88

12 Answers12

38

You might be interested in SHPAML:

http://shpaml.com/

I am actively maintaining it. It is a simple preprocessor, so it is not tied to any other tools like Genshi. I happen to use it with Django, so there is a little bit of Django support, but it should not interfere with most other use cases.

Armando Pérez Marqués
  • 5,661
  • 4
  • 28
  • 45
Steve Howell
  • 422
  • 4
  • 2
  • 4
    Is SHPAML still in active development? You seem to have given up maintainership and not much has happened since. I was considering forking it (without the rest of the website repo) on Github, but I'm not entirely sure whether this is a good idea as the PyPI package seems to be still around. – Alan Plum Jun 17 '11 at 19:13
  • 5
    According to the google groups mailing list the author is looking for a maintainer. I've found hamlpy (https://github.com/jessemiller) to be a little bit better, and development is still active on it. – bcoughlan Nov 06 '11 at 21:03
  • 1
    Annoying thing with this: there is no easy way to make empty elements like `
    ` without using HTML
    – Callum Rogers Sep 28 '12 at 16:51
21

i'm looking for the same. I haven't tried it, but found this:

http://github.com/jessemiller/HamlPy

Roger
  • 4,911
  • 6
  • 29
  • 26
  • 2
    This works great and also has a watcher much like SASS's one. Definitely more active than the top answer. – bcoughlan Nov 06 '11 at 21:01
  • 1
    This project is now basically dead; no commits in 2 years, no Python 3 support. Don't know how repo/package maintainers can give up control...but that should happen. – Nick T Feb 10 '15 at 01:33
  • @NickT Py3 support: hamlpy3 – Natacha Oct 01 '17 at 17:26
21

I'd check out GHRML, Haml for Genshi. The author admits that it's basically Haml for Python and that most of the syntax is the same (and that it works in Django). Here's some GHRML just to show you how close they are:

%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!
staackuser2
  • 12,172
  • 4
  • 42
  • 40
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
7

You may be interested in Plim. It's a mature python port of Slim.

Maxim Avanov
  • 131
  • 1
  • 3
4

This doesn't actually answer your question, but the CSS component of HAML, SASS, can be used freely with any framework. I'm using it right now with Django.

Sudhir Jonathan
  • 16,998
  • 13
  • 66
  • 90
  • Sudhir could you tell me about how you set this up? The installation uses "gem install" so I'm guess you downloaded the source and rigged it up some other way? DETAILS please! =] – carl crott Dec 14 '11 at 04:32
  • 1
    It's hardly that complicated... I'm using the Compass framework - it comes with a watch program that automatically compiles .scss files to .css. Take a look at http://compass-style.org/ – Sudhir Jonathan Dec 18 '11 at 09:51
3

My project, PyHAML, is a Mako preprocessor that gives you very HAML-like syntax. Some of the Ruby doesn't translate very well to Python, so there are some differences, but the spirit is the same.

Mike Boers
  • 6,665
  • 3
  • 31
  • 40
3

I ran across jade doing some node.js work and when returning to Django was looking for something similar. I found Pyjade and while it's nearly exactly what I'm looking for it handles errors and debugging rather poorly within Django.

Aaron Caito
  • 108
  • 6
  • This is fantastic. Should be the accepted answer. Still in active development at the time of this comment – Ben Aug 10 '15 at 03:50
3

I'm not sure what the status is of the GHRML bit as I only recently was looking into it. Can't find a repo for it, original developer doesn't have time for it anymore and maintenance was picked up by someone else with an interest in the project. Any extra info on this would be helpful.

Unfortunately, as these things go, I started writing my own HAML style processor ;)

http://dasacc22.wordpress.com/2010/05/03/python-implementation-of-haml/

It's in no way fit for production use at the moment (its literally a 4 day old infant now but like 60+hours crammed in those 4 days) but anyone interested may check it out here. It has a range of features already implemented that make it useful as a toy, and as I clean up the codebase over the next week, I hope to see it replace all my custom XSLT/xpath extensions and templates I have.

Daniel Skinner
  • 210
  • 4
  • 10
2

I just made this http://github.com/fitoria/django-haml it's still in very early stage but it works.

Fitoria
  • 605
  • 6
  • 13
2

Daniel Skinner's project (mentioned above) seems to have been renamed to DMSL. It seems very powerful, emulates HAML but allows you to use python functions, lambdas, comprehensions etc in it's syntax: https://github.com/dasacc22/dmsl

Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
2

I would use this one, it seems to be the most standard one: https://pypi.org/project/HamlPy3/0.83.0/

Assuming you're using Python3, you shouldn't haver any issues using it with Django, Flask or even standalone.

Ariel Monaco
  • 3,695
  • 1
  • 23
  • 21
1

You may want to try Hypertag: a new indentation-based language for HTML templating, strongly inspired by HAML. Hypertag has very clean syntax, provides full Django integration and a number of advanced features: native custom tags, DOM manipulation, Python-like imports, complex expressions, filters, control blocks (for, if, try...) and more. See here:

Disclaimer: I'm the author, glad to help if any issues or questions arise.

Marcin Wojnarski
  • 2,362
  • 24
  • 17