-
-
Save hchoroomi/58417 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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