<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Stephen Jackson]]></title>
  <link href="http://srayjackson.com/atom.xml" rel="self"/>
  <link href="http://srayjackson.com/"/>
  <updated>2011-11-20T16:06:38-06:00</updated>
  <id>http://srayjackson.com/</id>
  <author>
    <name><![CDATA[Stephen Jackson]]></name>
    <email><![CDATA[jackson.stephen.r@gmail.com]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Using Haml in Django Projects]]></title>
    <link href="http://srayjackson.com/blog/2011/10/23/using-haml-in-django-projects/"/>
    <updated>2011-10-23T16:45:00-05:00</updated>
    <id>http://srayjackson.com/blog/2011/10/23/using-haml-in-django-projects</id>
    <content type="html"><![CDATA[<p>Okay! So you are tired of writing all of those HTML tags in your templates? Well, if you are familiar with the Rails community you will know about Haml. Haml is a markup language that describes the HTML of a page in a cleaner and simpler way. With Haml you no longer have to specify closing tags. The closing tags are no longer needed because of indentation. All content indented under a tag is included within the HTML tag. I could go on and on about what Haml is and what the benefits are. For this, I will let the <a href="http://haml-lang.com/">Haml</a> site speak for itself. The purpose of this post is to explain how you would use Haml (HamlPy to be precise) in your django projects. There are two main ways to do this.</p>

<h2>First Method</h2>

<p>So far HamlPy is the best implementation of Haml for Python based projects that I&#8217;ve found thus far. Keep in mind that I am a Python and Django newbie so you may want to take this advice with a grain of salt but based on my research this is the case. The HamlPy project was created by <a href="https://github.com/jessemiller">Jesse Miller</a>. According to the documentation on the <a href="https://github.com/jessemiller/HamlPy">HamlPy project page</a> in github, HamlPy is not a template engine but rather a compiler that takes HamlPy markup and converts it to Django templates.</p>

<p>Okay, first things first. How do you install HamlPy? I will assume you already have Python installed and your favorite Python library installer (pip or easy_install). I primarily use pip. See command line examples below:</p>

<p>Using pip:</p>

<pre><code>pip install hamlpy
</code></pre>

<p>Using easy_install</p>

<pre><code>easy_install hamlpy
</code></pre>

<p>Next, create a page with a &#8220;hello.haml&#8221; file name. Place the following code in the file:</p>

<pre><code>%html
  %body
    %h1
      Hello World
</code></pre>

<p>To see a preview of the Haml to Django template conversion, open a command terminal and change directory to the location of your Haml file and issue the following command:</p>

<pre><code>hamlpy hello.haml
</code></pre>

<p>You should see the following output to the console:</p>

<pre><code>&lt;html&gt;
  &lt;body&gt;
    &lt;h1&gt;
      Hello World
    &lt;/h1&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>This is how you can preview the output of any HamlPy file. Okay, now you need to output the conversion to a file:</p>

<pre><code>hamlpy hello.haml hello.html
</code></pre>

<p>That&#8217;s great and all but more than likely you will have more than one HamlPy file that you will need converted. The command below will watch a entire directory:</p>

<pre><code>hamlpy-watcher your_folder
</code></pre>

<p>So, as you can see the first method of incorporating Haml in Django projects involves developing your templates in HamlPy and then converting them into Django templates. Using this method, you will simply deploy the compiled Django template files to your web server.</p>

<h2>Second Method</h2>

<p>For the second method, you will need to install <a href="https://github.com/chartjes/djaml">djaml</a> by <a href="https://github.com/chartjes">Chris Hartjes</a>. Djaml is a Django template loader that is used to incorporate HamlPy into Django projects.</p>

<p>First, for the install of djaml:</p>

<pre><code>pip install djaml
</code></pre>

<p>Next, add djaml to your template loaders your settings.py file:</p>

<pre><code>TEMPLATE_LOADERS = (
    'djaml.filesystem',
    ...
)
</code></pre>

<p>The djaml template loader must be first in the list or the normal Django template loaders will supercede djaml. Next, you develop your HamlPy template files like you did in the first method. Using djaml, your HamlPy files will be deployed to the web server. The Django templates are rendered in real time directly from the HamlPy files. In your views, you will reference the HamlPy files directly like in the example below:</p>

<pre><code>def my_view(request):
    ...
    return render(request, 'myapp/index.haml', {"foo": "bar"})
</code></pre>

<p>For HamlPy syntax, click <a href="https://github.com/jessemiller/HamlPy/blob/master/reference.md">here</a>. Enjoy Haml in Django!</p>
]]></content>
  </entry>
  
</feed>

