Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add all the shopify filters to standard liquid

We're using liquid in a web app. I've noticed Shopify have implemented some useful filters which are not included by default in the liquid gem. For example url_param_escape

To test it I did this:

$irb

require 'liquid' Liquid::Template.parse('{{ " & " | url_param_escape }} ').render => " & "

Clearly these filters are not included by default. Are they available from somewhere? If so where and how do I add them to the parser? Otherwise is t a case of implementing them all one by one or are they all coming from the same module or something?

like image 653
Will Avatar asked Nov 08 '22 20:11

Will


1 Answers

You can/must write them yourself. They're easy to create. Here's an example implementation of the url_param_escape filter:

module MyApp
  module Liquid
    module Filters
      module UrlParamFilter
        def url_param_escape(thing_to_escape)
          CGI.escape(thing_to_escape)
        end
      end
    end
  end
end

Then you'll need to register this filter so that Liquid knows to use it. I usually do this in application.rb inside config.after_initialize but there's probably a better place it can go if you have a lot of them. Here's an example of that:

config.after_initialize do
  ::Liquid::Template.register_filter(MyApp::Liquid::Filters::UrlParamFilter)
end
like image 197
Jimmy Baker Avatar answered Nov 15 '22 06:11

Jimmy Baker