Module: Goodmail::Layout

Extended by:
Layout
Included in:
Layout
Defined in:
lib/goodmail/layout.rb

Overview

Handles rendering the final HTML email by injecting the built content into the main layout template.

Constant Summary collapse

DEFAULT_LAYOUT_PATH =

Path to the default layout template within the gem

File.expand_path("layout.erb", __dir__)

Instance Method Summary collapse

Instance Method Details

#render(body_html, subject, layout_path: nil, unsubscribe_url: nil, preheader: nil) ⇒ String

Renders the email content within the layout template.

Parameters:

  • body_html (String)

    The HTML content generated by the Builder.

  • subject (String)

    The email subject line (used for the <title> tag).

  • layout_path (String, nil) (defaults to: nil)

    Optional path to a custom layout ERB file.

  • unsubscribe_url (String, nil) (defaults to: nil)

    Optional URL for the footer unsubscribe link.

  • preheader (String, nil) (defaults to: nil)

    Optional preheader text.

Returns:

  • (String)

    The full HTML document string.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/goodmail/layout.rb', line 21

def render(body_html, subject, layout_path: nil, unsubscribe_url: nil, preheader: nil)
  template_path = layout_path || DEFAULT_LAYOUT_PATH

  unless File.exist?(template_path)
    raise Goodmail::Error, "Layout template not found at #{template_path}"
  end

  template_content = File.read(template_path)

  # Use ERB#result_with_hash for cleaner variable passing (Ruby 2.5+)
  ERB.new(template_content).result_with_hash(
    body_html:       body_html,
    subject:         subject || "",
    config:          Goodmail.config, # Make config available
    unsubscribe_url: unsubscribe_url,  # Pass unsubscribe URL to template
    preheader:       preheader # Pass preheader to template
  )
rescue => e
  raise Goodmail::Error, "Failed to render layout template: #{e.message}"
end