Last active
July 28, 2022 16:48
-
-
Save brendancarney/0c189d667ca515a3a0dd514768addb1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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