Class: Bridgetown::PureRubyView

Inherits:
ERBView show all
Defined in:
lib/bridgetown-core/converters/ruby_templates.rb

Instance Attribute Summary

Attributes inherited from RubyTemplateView

#content, #layout, #paginator, #resource, #site

Instance Method Summary collapse

Methods inherited from ERBView

#h, #partial

Methods inherited from RubyTemplateView

#collections, #data, #data_key?, #helpers, #initialize, #inspect, #liquid_render, #method_missing, #partial, #respond_to_missing?, #site_drop

Methods included from Streamlined

#helper

Methods included from ERBCapture

#capture

Constructor Details

This class inherits a constructor from Bridgetown::RubyTemplateView

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bridgetown::RubyTemplateView

Instance Method Details

#_output_bufferObject



51
52
53
# File 'lib/bridgetown-core/converters/ruby_templates.rb', line 51

def _output_buffer
  @_erbout # might be nil
end

#_render_partial(partial_name, options) ⇒ Object

rubocop:todo Metrics



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bridgetown-core/converters/ruby_templates.rb', line 28

def _render_partial(partial_name, options) # rubocop:todo Metrics
  partial_path = _partial_path(partial_name, "rb")
  return super unless File.exist?(partial_path)

  (@_locals_stack ||= []).push(options)
  (@_buffer_stack ||= []).push(@_erbout)
  @_erbout = OutputBuffer.new

  site.tmp_cache["partial-tmpl:#{partial_path}"] ||= {
    signal: site.config.fast_refresh ? Signalize.signal(1) : nil,
  }
  tmpl = site.tmp_cache["partial-tmpl:#{partial_path}"]
  tmpl.template ||= options.keys.map do |k|
    "#{k}=locals[:#{k}];"
  end.push(File.read(partial_path)).join
  tmpl.signal&.value # subscribe so resources are attached to this partial within effect

  instance_eval(tmpl.template).to_s.tap do
    @_locals_stack.pop
    @_erbout = @_buffer_stack.pop
  end
end

#localsObject



55
56
57
# File 'lib/bridgetown-core/converters/ruby_templates.rb', line 55

def locals
  @_locals_stack&.last || {}
end

#render(item = nil, **options, &block) ⇒ Object

rubocop:disable Metrics



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bridgetown-core/converters/ruby_templates.rb', line 7

def render(item = nil, **options, &block) # rubocop:disable Metrics
  return @_erbout if !block && options.empty? && item.nil?

  if item.is_a?(Proc) || (block && item.nil?)
    result = item.is_a?(Proc) ? item.() : yield
    return result if result.is_a?(OutputBuffer)

    @_erbout ||= OutputBuffer.new
    @_erbout << result.to_s.html_safe

    return @_erbout
  end

  if item.respond_to?(:render_in)
    result = item.render_in(self, &block)
    result&.to_s&.html_safe
  else
    partial(item, **options, &block)&.html_safe
  end
end