Module: Bridgetown::Builders::DSL::Helpers

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

Instance Method Summary collapse

Instance Method Details

#helper(helper_name, method_name = nil, &block) ⇒ Object

Define a helper for use in view templates, alongside built-in helpers, using either a builder method or a block

Parameters:

  • helper_name (Symbol)

    name of the helper

  • method_name (Symbol) (defaults to: nil)

    name of a Builder method to use, if block isn’t provided and the method is named different from the helper



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bridgetown-builder/dsl/helpers.rb', line 17

def helper(helper_name, method_name = nil, &block)
  m = Module.new

  builder_self = self
  method_name ||= helper_name unless block
  unless method_name
    method_name = :"__helper_#{helper_name}"
    builder_self.define_singleton_method(method_name) do |*args, **kwargs, &block2|
      block.(*args, **kwargs, &block2)
    end
  end
  m.define_method helper_name do |*args, **kwargs, &block2|
    prev_var = builder_self.instance_variable_get(:@helpers)
    builder_self.instance_variable_set(:@helpers, self)
    builder_self.send(method_name, *args, **kwargs, &block2).tap do
      builder_self.instance_variable_set(:@helpers, prev_var)
    end
  end

  Bridgetown::RubyTemplateView::Helpers.include(m)

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

#helpersObject



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

def helpers
  @helpers # could be nil. gets set once a helper method is actually called
end