2015-06-19 40 views
1

我有許多層的應用程序,其中每層都有自己的液體模板。將液體數據傳遞到過濾器

現在我試圖實現一個過濾器,給定資產名稱返回它的url,就像shopify中的asset_url

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

什麼是使用每次調用時current_tier變量傳遞到過濾器最簡單的模式?

回答

1

使用上下文寄存器散列。

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