Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
Created February 4, 2009 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jamesmacaulay/58373 to your computer and use it in GitHub Desktop.
Save jamesmacaulay/58373 to your computer and use it in GitHub Desktop.
# haml + liquid example
#
# James MacAulay 2009
require 'rubygems'
require 'liquid'
require 'haml'
template = <<EOF
#title {{ title }}
#articles
{% for article in articles %}
.article
%h2.title article {{ forloop.index }}
.body
{{ article }}
{% endfor %}
EOF
engine = Haml::Engine.new(template, :suppress_eval => true)
haml_output = engine.render
# <div id='title'>{{ title }}</div>
# <div id='articles'>
# {% for article in articles %}
# <div class='article'>
# <h2 class='title'>article {{ forloop.index }}</h2>
# <div class='body'>
# {{ article }}
# </div>
# </div>
# {% endfor %}
# </div>
parsed_liquid = Liquid::Template.parse(haml_output)
assigns = {'title' => 'blah', 'articles' => ["<p>blah</p>\n<p>blah</p>\n<p>blah</p>", 'yadda yadda yadda']}
liquid_output = parsed_liquid.render(assigns)
# <div id='title'>blah</div>
# <div id='articles'>
#
# <div class='article'>
# <h2 class='title'>article 1</h2>
# <div class='body'>
# <p>blah</p>
# <p>blah</p>
# <p>blah</p>
# </div>
# </div>
#
# <div class='article'>
# <h2 class='title'>article 2</h2>
# <div class='body'>
# yadda yadda yadda
# </div>
# </div>
#
# </div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment