#118 Liquid

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

你可能感兴趣的:(html)