Liquid pass data to a filter

277 Views Asked by At

I have an application with many tiers, where each tier has its own liquid template.

Now I am trying to implement a filter that given an asset name returns its url, like the asset_url in shopify.

module UrlFilters
  def asset_url(input)
    current_tier.find_asset_by_name(input).url
  end
  # [...]
end

What is the simplest pattern to use to pass the current_tier variable to the filter each time it is called?

1

There are 1 best solutions below

0
On BEST ANSWER

Use Context registers hash.

module UrlFilters
  def asset_url(input)
    @context.registers[:current_tier].find_asset_by_name(input).url
  end
  # [...]
end

template = Liquid::Template.parse(some_template)
template.render({}, filters: [UrlFilters], registers: { current_tier: current_tier })