Kohaml – Haml Module for Kohana

I’ve been itching to use Haml with my new fav framework Kohana, however I was severely disappointed by the current state of haml interpreters for php. I had a free evening the other night and decided to write my own php haml interpreter and integrate it into a module for Kohana. The end result is tada! – Kohaml,  YAHOO!!! Okay while it may not save a life, Haml will definitely save you time and stress, as long as you can keep indentation monster off your back.

Download
Github

Kohaml takes this:


!!!
%html
  %head
    %title#content.wow{a => 'hello'} Hello World
  %body
    .wow hhaha
      %strong{'class' => "code", 'id' => "message"} Hello, World! <? 'howdy' ?>
      %b Hello World
      %peanutbutterjelly
        / This is the peanutbutterjelly element
        .helloa <?= $wow ?>
        #woa was
        %br/
    #foo=   $wow
      hahaa
      %b{'class' => 'type'}= $hello
    %sandwich{'delicious' => 'true', 'more' = 'okay'}
    .wowaa he
      |<?php echo('wuttup'); ?>
    |<?php echo('wuttup'); ?>
      |<?php echo('wuttup'); ?>
        #wow.escape
      |<?php echo('okay') ?>
    |<?php echo('wuttup'); ?>
    %ul
      %li <?php 'hello' ?> jadsf <?php 'aa' ?>
      %li Pepper
      / heloaf

And turns it into this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title "class"="wow" "id"="content" "a"="hello">Hello World</title>
  </head>
  <body>
    <div "class"="wow">
      hhaha
      <strong "class"="code" "id"="message">Hello, World! <? 'howdy' ?></strong>
      <b>Hello World</b>
      <peanutbutterjelly>
        <!-- This is the peanutbutterjelly element -->
        <div "class"="helloa"><?= $wow ?></div>
        <div "id"="woa">wasaa</div>
        <br/>
      </peanutbutterjelly>
    </div>
    <div "id"="foo">
      <?= $wow ?>
      hahaa
      <b "class"="type"><?= $hello ?></b>
    </div>
    <sandwich "delicious"="true" "more"="okay"></sandwich>
    <div "class"="wowaa">
      he
      <?php echo('wuttup'); ?>
    </div>
    <?php echo('wuttup'); ?>
      <?php echo('wuttup'); ?>
        <div "class"="escape" "id"="wow"></div>
      <?php echo('okay') ?>
    <?php echo('wuttup'); ?>
    <ul>
      <li><?php 'hello' ?> jadsf <?php 'aa' ?></li>
      <li>Pepper</li>
      <!-- heloaf -->
    </ul>
  </body>
</html>

Here is a brief overview of how to use Kohaml. The above example makes use of all the rules, check out http://SERVER_NAME/kohaml for a demo.

Rules:

  • % – Define an element tag: %b = <b>. If using % it must be at the start of line. Can be used with . and #.
  • # – Id. #content = <div id=”content”></div>. Can be used with . and %. Multiple #’s can be used.
  • . – Class. .my_rows = <div class=”my_rows”></div>. Can be used with # and %. Multiple .’s can be used.
  • { }  – specifies attributes for that element. %sandwich{‘delicious’ = ‘true’, ‘more’ = ‘okay’} = <sandwich “delicious”=”true” “more”=”okay”></sandwich>
  • <? – At the beginning of a line specifies a line of php code and the line and depth are ignored.
  • | – At the beginning of a line of php code will take that line of php into account. Use | for nesting php code within elements.
  • / – At the beginning of a line defines a comment. Comments can be nested.
  • / – At the ending of line creates a self-closing tag. %br/ = <br/>

Guidelines:

  • Putting text at the end of element declaration will wrap it. #element myText = <element>myText</element>
  • To nest element or text use 2 spaces. Haml is indent sensitive. Kohaml will raise an error on incorrect indentation.
  • Any php within a line (not at the start of a line) will be kept intact

Shortcuts:

  • #element= $var = <element><?= $var ?></element>
  • load(myTemplate) will generate a function to load a sub-view into the current file. Will also nest and indent appropriately. Declared at the beginning of the line.
  • #elment= load(myTemplate) = will load a haml template and nest it within the element
This entry was posted in Programming and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

12 Comments

  1. Bobby Prabowo
    Posted December 22, 2008 at 10:43 pm | Permalink

    there’s a bug
    if i use this line
    $content = $this->pagination->render(‘digg’);

    Kohana will show

    InvalidArgumentException

    application/libraries/KohamlLib.php [79]:
    Passed variable is not an array or object, using empty array instead;

    it makes me can’t use the pagination

  2. Justin Hernandez
    Posted December 23, 2008 at 12:21 am | Permalink

    Sorry abou thtat. Had to update the error message for a missing template. Basically it’s saying there is no system/views/pagination/digg.haml template.

  3. Bobby Prabowo
    Posted December 23, 2008 at 5:44 am | Permalink

    Wow.
    What a fast update.

    Keep the great work.

    Thanks

  4. Posted May 25, 2009 at 11:41 am | Permalink

    Please excuse a silly question from a brand-new Kohana user. I’m not quite sure how to get this module working, per your instruction:

    “To load a haml template just use View as you normally would.”

    I’ve installed the module files into the modules directory, added ‘haml’ to the modules array in config.php, and have renamed views as .haml files. What else should I do or what should I be doing differently?

  5. adam
    Posted June 26, 2009 at 4:52 am | Permalink

    This looks really nice, but one of the reasons I love HAML is that there are no angle brackets!

    Do you think it would be possible to mimic the Rails convention (also seen in php versions) of using = to stand in for the tag?

    for example:

    = echo(‘hello’)

    would yield:

  6. Posted July 30, 2009 at 8:32 pm | Permalink

    Just passing“`

  7. Posted August 8, 2009 at 9:20 pm | Permalink

    Hey Justin, now that you’re working on a Haml implementation, you should join the haml-dev mailing list: http://groups.google.com/group/haml-dev

  8. Posted December 10, 2009 at 2:32 am | Permalink

    metin2 yang

  9. Dan
    Posted January 3, 2010 at 9:12 pm | Permalink

    aren’t you thinking about rewriting kohaml module to kohana v3?

  10. Justin Hernandez
    Posted January 5, 2010 at 9:57 pm | Permalink

    Yeah I’m going to port it over to K3. Thanks for the interest. :)

  11. Dan
    Posted January 7, 2010 at 12:33 pm | Permalink

    maybe you need any help with porting it?

  12. Dan
    Posted January 27, 2010 at 12:50 am | Permalink

    when are you planing to port it?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>