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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With