Skip to content

Instantly share code, notes, and snippets.

Last active March 17, 2016 14:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Ejemplo Liquid
# los drops son para definir que se puede exponer en un template
class ProductDrop < Liquid::Drop
def initialize(product)
@product = product
def name
def price
# los filters nos permiten definir "helpers" para los templates
class PriceFilter
def to_price(input)
"$ #{input}"
# le decimos a liquid que vamos a usar este filter
template_string = %(
{% for product in products %}
<p>{{}} - {{product.price | to_price}}</p>
{% end %}
# un array de hash para este ejemplo, pero en spincommerce usamos models
products = [
{ name: 'Skate', price: 5000},
{ name: 'Wheels', price: 2000}
# acá ocurre toda la magia
liquid_template = Liquid::Template.parse(template_string)
# seteamos los objectus que se usan en el contexto del template
liquid_template.assigns['products'] ={ |product| }
# hacemos el render, que nos devuelve un string con el html parseado
# nos devuelve esto
# <p>Skate - $ 5000</p>
# <p>Wheels - $ 2000</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment