Class: Bridgetown::Converters::LiquidTemplates

Inherits:
Bridgetown::Converter show all
Defined in:
lib/bridgetown-core/converters/liquid_templates.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Bridgetown::Converter

#initialize, input, #inspect, #line_start, support_slots, supports_slots?

Methods inherited from Plugin

#initialize

Methods included from Prioritizable

#<=>, included

Constructor Details

This class inherits a constructor from Bridgetown::Converter

Class Attribute Details

.cached_partialsObject

Returns the value of attribute cached_partials.



12
13
14
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 12

def cached_partials
  @cached_partials
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



9
10
11
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 9

def document
  @document
end

#layoutObject (readonly)

Returns the value of attribute layout.



9
10
11
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 9

def layout
  @layout
end

#siteObject (readonly)

Returns the value of attribute site.



9
10
11
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 9

def site
  @site
end

Instance Method Details

#configure_payload(content = nil) ⇒ Object

Set page content to payload and assign paginator if document has one.

Returns nothing



87
88
89
90
91
92
93
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 87

def configure_payload(content = nil)
  payload["page"] = document.to_liquid
  payload["paginator"] = document.respond_to?(:paginator) ? document.paginator.to_liquid : nil
  payload["layout"] = @layout ? @layout.to_liquid.merge({ data: @layout.data }) : {}
  payload["content"] = content
  payload["data"] = payload["page"].data
end

#convert(content, convertible) ⇒ String

Logic to do the Liquid content conversion.

Parameters:

Returns:

  • (String)

    The converted content.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 26

def convert(content, convertible)
  return content if convertible.data[:template_engine] != "liquid"

  self.class.cached_partials ||= {}
  @payload = nil

  @site = convertible.site
  if convertible.is_a?(Bridgetown::Layout)
    @document = convertible.current_document
    @layout = convertible
    configure_payload(layout.current_document_output)
  else
    @document = convertible
    @layout = site.layouts[document.data["layout"]]
    configure_payload
  end

  template = site.liquid_renderer.file(convertible.path).parse(content)
  template.warnings.each do |e|
    Bridgetown.logger.warn "Liquid Warning:",
                           LiquidRenderer.format_error(e, convertible.path)
  end
  template.render!(payload, liquid_context)
# rubocop: disable Lint/RescueException
rescue Exception => e
  Bridgetown.logger.error "Liquid Exception:",
                          LiquidRenderer.format_error(e, convertible.path)
  raise e
end

#liquid_contextObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 95

def liquid_context
  {
    registers: {
      site: site,
      page: payload["page"],
      cached_partials: self.class.cached_partials,
    },
    strict_filters: site.config["liquid"]["strict_filters"],
    strict_variables: site.config["liquid"]["strict_variables"],
  }
end

#matches(ext, convertible) ⇒ Object

Parameters:



61
62
63
64
65
66
67
68
69
70
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 61

def matches(ext, convertible)
  if convertible.render_with_liquid?
    convertible.data[:template_engine] = "liquid"
    return true
  end

  super(ext).tap do |ext_matches|
    convertible.data[:template_engine] = "liquid" if ext_matches
  end
end

#output_ext(ext) ⇒ Object



72
73
74
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 72

def output_ext(ext)
  ext == ".liquid" ? ".html" : ext
end

#payloadObject

Fetches the payload used in Liquid rendering. Falls back to site.site_payload if no payload is set.

Returns a Bridgetown::Drops::UnifiedPayloadDrop



80
81
82
# File 'lib/bridgetown-core/converters/liquid_templates.rb', line 80

def payload
  @payload ||= site.site_payload
end