Module: Goodmail::Layout
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.("layout.erb", __dir__)
Instance Method Summary collapse
-
#render(body_html, subject, layout_path: nil, unsubscribe_url: nil, preheader: nil) ⇒ String
Renders the email content within the layout template.
Instance Method Details
#render(body_html, subject, layout_path: nil, unsubscribe_url: nil, preheader: nil) ⇒ String
Renders the email content within the layout template.
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 |