diff --git a/bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb b/bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb index a43d599b2..1e13eb4fa 100644 --- a/bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb +++ b/bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb @@ -5,10 +5,18 @@ module Builders module DSL module Liquid def liquid_filter(filter_name, method_name = nil, &block) - block = method(method_name) if method_name.is_a?(Symbol) - + builder_self = self m = Module.new - m.send(:define_method, filter_name, &block) + + if block + m.define_method filter_name do |*args| + builder_self.instance_exec(*args, &block) + end + elsif method_name + block = method(method_name) + m.define_method filter_name, &block + end + ::Liquid::Template.register_filter(m) functions << { name: name, filter: m } diff --git a/bridgetown-builder/test/test_filters_dsl.rb b/bridgetown-builder/test/test_filters_dsl.rb index b95d31eb2..c64cf8640 100644 --- a/bridgetown-builder/test/test_filters_dsl.rb +++ b/bridgetown-builder/test/test_filters_dsl.rb @@ -16,6 +16,10 @@ def build value = input * multiply_by add_by ? value + add_by : value end + + liquid_filter "site_config" do |input| + input.to_s + " #{site.root_dir}" + end end end @@ -48,5 +52,11 @@ class TestFilterDSL < BridgetownUnitTest result = Liquid::Template.parse(content).render assert_equal "5 times 10 plus 3 equals 53", result end + + should "allow access to local builder scope" do + content = "root_dir: {{ 'is' | site_config }}" + result = Liquid::Template.parse(content).render + assert_equal "root_dir: is #{@site.root_dir}", result + end end end