ITeye博客

#118 Liquid

CaiDeHen · 2008-12-12 16:18 · Railscasts · Markdown

Liquid is a safe way to provide a powerful template language to the site's users. See how in this episode.

<!-- 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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。