Skip to content

hybridious/pyhaml

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#pyHaml

deprecated: do not use.

pyHaml is a python port of Haml, an HTML templating engine used primarily with Ruby on Rails. Ruby Haml will be referred to as rHaml for the purposes of this document.

In order to make pyHaml a bit more pythonic, most of the syntax evaluated as Ruby in rHaml is evaluated as python. For example, instead of using ruby hashes for attributes pyhaml uses python dict syntax:

%tagname{'attr1': 'value1', 'attr2': 'value2'} Contents

pyHaml aims to be flexible and intuitive, allowing python to be evaluated inline as would be expected.

-def foo(i):
  %p = i ** 2
-for i in range(4):
  -foo(i)

yields

<p>0</p>
<p>1</p>
<p>4</p>
<p>9</p>

By allowing haml inside of python code blocks, some handy functionality can be produced through function composition. For instance, to wrap the output of a function in some predefined html one could use the following construct:

-def wrap(f):
  .wrapper
    %p
      -f()

-def foo():
  foo

-wrap(foo)

which produces the following html:

<div class="wrapper">
  <p>
    foo
  </p>
</div>

#imports

Markup should be reused just like code (since it is code). In this vein there should be some way to use one haml document from within another. In the spirit of python, pyHaml does this using the import statement. For instance, assuming the following two documents:

-# foo.haml
-def foo():
  %p foo

-# bar.haml
-import foo
-foo.foo()

rendering bar.haml produces <p>foo</p>. Some versions of python, particularly the one used in google appengine, cache imports. This makes it impossible to use import for this purpose. For this reason, the _haml.imp method is provided instead. In the previous example the line -import foo would be written as -foo = _haml.imp('foo').

#portability

pyHaml runs on python 2.5, 2.6, 2.7 and the latest version of python 3.

About

a haml compiler in python

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.5%
  • Other 0.5%