Skip to content

Instantly share code, notes, and snippets.

@brendancarney
Last active July 28, 2022 16:48
Show Gist options
  • Save brendancarney/0c189d667ca515a3a0dd514768addb1a to your computer and use it in GitHub Desktop.
Save brendancarney/0c189d667ca515a3a0dd514768addb1a to your computer and use it in GitHub Desktop.
html = <<~HTML
{% assign message = "hello" %}
<div>
{% product 123 %}
<div>{{ product.title }}</div>
{% endproduct %}
{% product 456 %}
Liquid works inside these blocks
<div>{{ message }}</div>
<div>{{ product.title }}</div>
{% endproduct %}
Shouldn't render:
{{ product.title }}
</div>
HTML
class ProductLiquid < Liquid::Block
def initialize(tag_name, markup, tokens)
super
# We can look up the product here, which could be cached.
# Alterntively, we could use the Liquid::Document to find all
# product tags and pull the ids ahead of time to do a single lookup.
@product_id = markup.to_i
puts @product_id
end
def render(context)
output = ""
context.stack do
context["product"] = { "title" => @product_id }
output = super(context)
end
output
end
end
Liquid::Template.register_tag("product", ProductLiquid)
Liquid::Template.parse(html).render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment