Module: Bridgetown::Builders::DSL::Liquid

Included in:
PluginBuilder
Defined in:
bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb

Instance Method Summary collapse

Instance Method Details

#filtersObject

[View source]

7
8
9
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb', line 7

def filters
  @filters # could be nil
end

#filters_contextObject

[View source]

11
12
13
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb', line 11

def filters_context
  filters&.instance_variable_get(:@context)
end

#liquid_filter(filter_name, method_name = nil, filters_scope: false, &block) ⇒ Object

[View source]

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb', line 15

def liquid_filter(filter_name, method_name = nil, filters_scope: false, &block)
  m = Module.new

  if block && filters_scope
    Deprecator.deprecation_message(
      "The `filters_scope' functionality is deprecated. Use the `filters' builder " \
      "method to access the filters scope in your plugin."
    )
    m.define_method filter_name, &block
  else
    builder_self = self
    method_name ||= filter_name unless block
    unless method_name
      method_name = :"__filter_#{filter_name}"
      builder_self.define_singleton_method(method_name) do |*args, **kwargs|
        block.(*args, **kwargs) # rubocop:disable Performance/RedundantBlockCall
      end
    end
    m.define_method filter_name do |*args, **kwargs|
      prev_var = builder_self.instance_variable_get(:@filters)
      builder_self.instance_variable_set(:@filters, self)
      builder_self.send(method_name, *args, **kwargs).tap do
        builder_self.instance_variable_set(:@filters, prev_var)
      end
    end
  end

  ::Liquid::Template.register_filter(m)

  functions << { name:, filter: m }
end

#liquid_tag(tag_name, method_name = nil, as_block: false, &block) ⇒ Object

[View source]

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/liquid.rb', line 47

def liquid_tag(tag_name, method_name = nil, as_block: false, &block)
  method_name ||= tag_name unless block
  block = method(method_name) if method_name
  local_name = name # pull the name method into a local variable

  tag_class = as_block ? ::Liquid::Block : ::Liquid::Tag
  tag = Class.new(tag_class) do
    define_method(:_builder_block) { block }
    define_singleton_method(:custom_name) { local_name }

    def inspect
      "#<#{self.class.custom_name} (Liquid Tag)>"
    end

    attr_reader :content, :context

    def render(context)
      @context = context
      @content = super if is_a?(::Liquid::Block)
      _builder_block.call(@markup.strip, self)
    end
  end

  ::Liquid::Template.register_tag tag_name, tag
  functions << { name:, tag: [tag_name, tag] }
end