bukk530
bukk530

Reputation: 1895

Liquid pass data to a filter

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?

Upvotes: 1

Views: 268

Answers (1)

Michał Młoźniak
Michał Młoźniak

Reputation: 5556

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 })

Upvotes: 1

Related Questions