#118 Liquid
Liquid is a safe way to provide a powerful template language to the site's users. See how in this episode.
- Download:
- source codeProject Files in Zip (101 KB)
- mp4Full Size H.264 Video (25.3 MB)
- m4vSmaller H.264 Video (17 MB)
- webmFull Size VP8 Video (43 MB)
- ogvFull Size Theora Video (37.3 MB)
Resources
pages/show.html.erb
<%= liquidize @page.content, 'page' => @page %>
<%= liquidize @page.content, 'page' => @page %>
environment.rb
config.gem 'liquid'
config.gem 'liquid'
application_helper.rb
def liquidize(content, arguments)
RedCloth.new(Liquid::Template.parse(content).render(arguments, :filters => [LiquidFilters])).to_html
end
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
module LiquidFilters include ActionView::Helpers::NumberHelper def currency(price) number_to_currency(price) end end
category.rb
liquid_methods :name
liquid_methods :name
page.rb
liquid_methods :products
def products
Product.all
end
liquid_methods :products def products Product.all end
product.rb
liquid_methods :name, :price, :category
liquid_methods :name, :price, :category
bash
rake gems:install
gem server
rake gems:install gem server
liquid
# page liquid content
{% for product in page.products %}
*{{ product.name }}* {{ product.price | currency }}
Category: {{ product.category.name }}
{% endfor %}
# page liquid content
{% for product in page.products %}
*{{ product.name }}* {{ product.price | currency }}
Category: {{ product.category.name }}
{% endfor %}
