#118
Jul 14, 2008

Liquid

Liquid is a safe way to provide a powerful template language to the site's users. See how in this episode.
Tags: plugins views
Download (30.2 MB, 16:04)
alternative download for iPod & Apple TV (21.4 MB, 16:04)

Resources

<!-- pages/show.html.erb -->
<%= liquidize @page.content, 'page' => @page %>
# environment.rb
config.gem 'liquid'

# application_helper.rb
def liquidize(content, arguments)
  RedCloth.new(Liquid::Template.parse(content).render(arguments, :filters => [LiquidFilters])).to_html
end

# lib/liquid_filters.rb
module LiquidFilters
  include ActionView::Helpers::NumberHelper
  
  def currency(price)
    number_to_currency(price)
  end
end

# category.rb
liquid_methods :name

# page.rb
liquid_methods :products

def products
  Product.all
end

# product.rb
liquid_methods :name, :price, :category
rake gems:install
gem server
# page liquid content
{% for product in page.products %}
*{{ product.name }}* {{ product.price | currency }}
Category: {{ product.category.name }}
{% endfor %}

Full Source Code

RSS Feed for Episode Comments 16 comments

1. Morten K. Holst Jul 14, 2008 at 01:22

That's pretty cool.

I actually just happened to need the number_to_currency action.

Thanks.


2. leethal Jul 14, 2008 at 02:19

And I was thinking Liquid was all outdated and stuff. Niceness!


3. Jeremy Bise Jul 14, 2008 at 07:37

Thanks so much for posting this! I'd been looking for something like it for quite sometime. :) You're a hero.


4. Matthijs Langenberg Jul 14, 2008 at 11:11

Thanks Ryan, this gives me a head-start for the templating system in a SaaS project.


5. tim morgan Jul 14, 2008 at 14:58

Hey Ryan, your product names are a little strange! What happened?


6. pillar Jul 14, 2008 at 16:58

[code:ruby]def thanks
puts "Thanks Ryan"
end[/code]


7. Ryan Bates Jul 14, 2008 at 17:03

@leethal, Liquid is pretty old, but AFAIK it's still the best at what it does. If anyone knows of alternatives I'd love to hear.

@Matthijs, they're just words from the filler text "lorem ipsum". I got tired of thinking up good names. ;)


8. James Throgmorton Jul 14, 2008 at 17:25

I can't tell you how much I look forward to every monday. I am subscribing to your pragmatic screencasts, and I get them as soon as they come out as well. Thank you for the discipline and the excellence that you have defined in your work.


9. RubyGeek Jul 15, 2008 at 01:57

Thanks for the cool screencast on Liquid.Will be looking foward for more screencast in liquid especially on how to use common layouts in liquid templates!


10. Jose Jul 15, 2008 at 05:13

Ryan, what about 2 episodes per week? :P


11. Jean-Marc Jul 15, 2008 at 07:28

Bryan,

appart from the fact that it might be the only one doing what Liquid does... it is to my knowledge the only templating language being used on a site where the users can change their own look and feel using it (which says a lot about how safe it is).


12. Emmanuel Oga Jul 25, 2008 at 21:49

Thanks for tutorial. I recently wrote <a href="http://emmanueloga.wordpress.com/2008/07/26/liquid-coolness/">a blog post</a> about liquid internals, I hope you guys may find it useful.

Greets!


13. Austin Schneider Aug 13, 2008 at 11:59

I see how you added custom filters, but what about custom tags? I tried creating a file in libs containing

module LiquidTags
class X < Liquid::Tags

end
end

but that didn't seem to be the right way to do it. What seems to work is freezing the gem and putting a new file in 'vendor/liquid-x.x.x/lib/liquid/tags'

I don't like doing it that way, but is that the best way to do it?


14. Sascha Sep 02, 2008 at 06:46

Hi Ryan,
first of all 'Thank you for these amazing screencasts'!

You asking for alternatives in one of the previous posts. Have you heard from HAML http://haml.hamptoncatlin.com/ ? Is it an alternativ to Liquid for you? In my opinion it has some interesting approachs.


15. Mikael Oct 22, 2008 at 07:06

I second that #13. I have yet to find anything on how to use liquid tags. Anyone out there that has a clue?


16. cheap wow gold Nov 05, 2008 at 01:15

amazing news,thank you

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player