From a77ff3c06288257ff57136725e2bae6af2e8d797 Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Thu, 14 Jul 2022 11:04:31 -0700 Subject: [PATCH] Adding the ability to use a layout for your html templates. (#70) * Adding the ability to use a layout for your html templates. Fixes #69 * apparently I can't do a comment here... --- spec/email_spec.cr | 11 ++++++++++ spec/templates/custom_layout/layout.ecr | 3 +++ spec/templates/email_with_layout/html.ecr | 1 + src/carbon/email.cr | 25 ++++++++++++++++++++--- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 spec/templates/custom_layout/layout.ecr create mode 100644 spec/templates/email_with_layout/html.ecr diff --git a/spec/email_spec.cr b/spec/email_spec.cr index c09b039..b995b78 100644 --- a/spec/email_spec.cr +++ b/spec/email_spec.cr @@ -73,6 +73,11 @@ private class EmailWithCustomAttributes < Carbon::Email end end +private class EmailWithLayout < BareMinimumEmail + templates html + layout custom_layout +end + describe Carbon::Email do it "can build a bare minimum email" do email = BareMinimumEmail.new @@ -135,4 +140,10 @@ describe Carbon::Email do it "normalizes recipients" do end + + it "includes a layout" do + email = EmailWithLayout.new + email.html_body.should contain "Email Layout" + email.html_body.should contain "Email body" + end end diff --git a/spec/templates/custom_layout/layout.ecr b/spec/templates/custom_layout/layout.ecr new file mode 100644 index 0000000..2b27b2a --- /dev/null +++ b/spec/templates/custom_layout/layout.ecr @@ -0,0 +1,3 @@ +

Email Layout

+ +<%= content %> \ No newline at end of file diff --git a/spec/templates/email_with_layout/html.ecr b/spec/templates/email_with_layout/html.ecr new file mode 100644 index 0000000..47b6423 --- /dev/null +++ b/spec/templates/email_with_layout/html.ecr @@ -0,0 +1 @@ +

Email body

\ No newline at end of file diff --git a/src/carbon/email.cr b/src/carbon/email.cr index 4c85983..ec84ced 100644 --- a/src/carbon/email.cr +++ b/src/carbon/email.cr @@ -19,20 +19,39 @@ abstract class Carbon::Email def text_body; end + def text_layout(content_io : IO); end + def html_body; end + def html_layout(content_io : IO); end + getter headers macro inherited macro templates(*content_types) \{% for content_type in content_types %} def \{{ content_type }}_body : String - io = IO::Memory.new - ECR.embed "#{__DIR__}/templates/\{{ @type.name.underscore.gsub(/::/, "_") }}/\{{ content_type }}.ecr", io - io.to_s + content_io = IO::Memory.new + ECR.embed "#{__DIR__}/templates/\{{ @type.name.underscore.gsub(/::/, "_") }}/\{{ content_type }}.ecr", content_io + \{{ content_type }}_layout(content_io) || content_io.to_s end \{% end %} end + + # Specify an HTML template layout + # + # ``` + # templates html + # layout email_layout + # ``` + macro layout(template_name) + def html_layout(content_io : IO) + content = content_io.to_s + layout_io = IO::Memory.new + ECR.embed "#{__DIR__}/templates/\{{ template_name.id }}/layout.ecr", layout_io + layout_io.to_s + end + end end @headers = {} of String => String