#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 39 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. Jens Feb 12, 2009 at 23:03

It seems this won't work with url helpers? Doing something like posts_path returns "undefined method `default_url_options’" even when including UrlWriter and UrlHelper first. Or maybe I'm missing something.... :/


17. Rathgar Feb 16, 2009 at 10:44

Really useful for dipping your toes into the 'liquid' world (sorry)

I've been struggling for about a week with URL generation which is a deal-breaker for most uses. Still no luck yet.

Anyone?


18. Dom Aug 27, 2009 at 16:54

Hi Ryan, thanks for all your screencasts.

I was wondering about the bit where you edited the content of the textarea from Safari in Textmate. Do you have a plugin for Safari where you can edit in Textmate?


19. US Promo Code Oct 29, 2009 at 13:04

Yeah I also have something to add. It's here.


20. Peter Dec 05, 2009 at 08:48

A lot is talked about Jquery these days but is it worth making the switch? You also mention Unobtrusive javascript. What does it mean?


21. rapid4me Dec 29, 2009 at 06:46

Excellent post! I always enjoy a solid technical post (and code) It saved me a good week+ - Keep up the good work!


22. Rar Password Jan 04, 2010 at 15:13

I see how you added custom filters, but what about custom tags?


23. web-braga Mar 25, 2010 at 19:26

Helpful information you have available! Your post helped me save time on development.


24. lace wigs May 13, 2010 at 20:20

is a good site


25. Carmelo May 13, 2010 at 20:21

thanks


26. bonus codes Jun 17, 2010 at 23:30

Thanks for sharing the information. That’s a awesome article you posted. I found the post very useful as well as interesting. I will come back to read some more.


27. buy steroids Jun 20, 2010 at 00:45

well worth the read.I found it very informative as I have been researching a lot lately on practical matters such as Liquid...


28. spam Jun 29, 2010 at 09:08

I really appreciate what you post.


29. Pai You Guo Jun 29, 2010 at 22:48

Hey Ryan, thanks for all your screencasts.I was wondering about the bit where you edited the content of the textarea from Safari in Textmate.

Do you have a plugin for Safari where you can edit in Textmate?


30. cardsharing server Jul 14, 2010 at 10:50

I love it,Excellent article.I am decide to put this into use one of these days.Thank you for sharing this.To Your Success


31. free card sharing Jul 14, 2010 at 10:54

Great,I think it's very useful.DMOZ4U is a quality Free Web Directory


32. card sharing Jul 14, 2010 at 10:57

Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!


33. chanel shoes Jul 23, 2010 at 01:41

from us with full confidence.
 Free shipping to your door!


34. DVD to iPad Converter Jul 25, 2010 at 18:47

ah h a,this is the best


35. asics shose Jul 27, 2010 at 20:16

Thanks a lot for enjoying this beauty article with me.


36. asics shose Jul 28, 2010 at 00:21

Thanks a lot for enjoying this beauty article with me.


37. Start Sharing Not Selling Aug 02, 2010 at 10:08

Very interesting screencast ! Cool new feature ! Thanks for this tip !


38. free directory list Aug 11, 2010 at 22:28

Just to echo what others have said, after multiple searches across the web this solved my check_box problem in five minutes. Thanks so much!


39. daies Aug 18, 2010 at 05:19

Nice article written. Certainly much patience it. At night writing the need for a <a href = "http://www.tomtop.com/charger-station-4-x-battery-for-wii- remote-controller_p2950.html "> Wii Remote Charger </a> to ensure that you have to be efficient?


40. Wii Remote Charger Aug 18, 2010 at 05:20

Nice article written. Certainly much patience it. At night writing the need for a Wii Remote Charger to ensure that you have to be efficient?


41. replicahandbags Aug 20, 2010 at 00:33

I have always liked Outdoor movies, a child standing at the window, looked out from home to

the following. Will be able to see the staff busy figure, a huge white cloth has a child

hang up and soon will be able to see the movie.


42. Handcrafted Jewelry Maryland Aug 20, 2010 at 07:26

Thanks for the help. I really enjoyed the video and I think it will be very useful information to know. I look forward to learning more about the subject.


43. Wholesale hats Aug 20, 2010 at 21:18

I recently came across your blog and have been reading along.
I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading.Nice blog,I will keep visiting this blog very often.


44. nike dunks low Aug 20, 2010 at 22:52

Thanks for the code. Good article, Each and every point is good enough. I really enjoy watching the RailsCasts. I think type of site that is useful in sharing information and it is important to share. Great site. This could probably have the refactoring tag added t it.


45. Alexander McQueen Store Aug 21, 2010 at 00:58

I like this article


46. free microsoft word Aug 23, 2010 at 05:01

nice article


47. lawsuit advance Aug 24, 2010 at 23:35

Really impressed! Everything is very open and very clear explanation of issues. It contains truly information. Your website is very useful. Thanks for sharing. Looking forward to more!


48. Shanda Carles Aug 26, 2010 at 07:17

You write very detailed,Pay tribute to you.Couldn’t be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!


49. louis vuitton shoes Aug 26, 2010 at 21:14

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


50. Wholesale Electronics Aug 27, 2010 at 00:46

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


51. neverfull Aug 29, 2010 at 23:01

Nice article written. Certainly much patience it. At night writing the need for a Wii Remote Charger to ensure that you have to be efficient?


52. snow boots Aug 31, 2010 at 00:14

I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!


53. free microsoft word Aug 31, 2010 at 09:55

pretty nice article written. Good for rookies


54. levis belts Sep 01, 2010 at 20:37

Thanks for sharing your article.


55. reverse phone detective Sep 02, 2010 at 07:07

Thank you for the discipline and the excellence that you have defined in your work.


56. batteries Sep 02, 2010 at 07:36

i love your articles youre always giving me great ideas on how to progress with my blog. thanks a lot for keeping us informed.

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
Give Back to Open Source