如何在 .rb 文件中呈现邮件程序模板(无模板文件)
How do I render a mailer template within the .rb file (no template file)
部分原因是 React 非常方便,可以在组件中添加一小段模板代码。这是我目前所拥有的:
# app/mailers/membership_mailer.rb
# frozen_string_literal: true
class MembershipMailer < ApplicationMailer
def one_week_expiration(member, renewal, org)
template = slim(%(
= content_for :header
= org.name
Hello, #{member.name},
br
Your membership for #{org.name} is set to expire at
#{renewal.expires_at.to_s(:short)}.
br
Please visit <a href=#{org.url}>#{org.url}</a> to purchase
a membership renewal.
br
br
))
subject = "Your membership for #{org.name} is going to expire soon"
mail(to: member.email, subject: subject) do |format|
# there isn't a slim renderer
format.html { render text: template }
end
end
end
我的所有电子邮件都使用此布局,并将其设置为默认布局 application_mailer。
# app/views/layouts/email.html.slim
= stylesheet_link_tag "email"
header
.shell
h1 = yield :header
.shell
br
= yield :body
= yield
= render partial: "/shared/email_footer"
我想要这里的所有呈现方法,但我正在努力解决的问题是在哪里可以找到/弄清楚如何构建邮件程序模板、布局,并让所有我想要访问的变量都通过到模板。
# app/mailers/application_mailer.rb
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: APPLICATION_CONFIG['support_email']
layout 'email'
# taken from slim/test/block rendering / helper
def slim(source, options = {}, &block)
scope = options.delete(:scope)
locals = options.delete(:locals)
Slim::Template.new('', {}) { source }.render(scope, locals, &block)
end
end
最终,我会有 erb、arbre 等
所以,总而言之,当我调用 mail(...){...} 时,我希望能够在 ruby 中定义我的模板,而不是在模板文件中,因为我不喜欢邮件程序和模板相隔太远(在文件系统中..为什么不在这个问题的范围内,我只想解决 ruby 中的渲染问题现在)。
看起来最简单的方法是使用 render_anywhere,这只是一个非常精简的控制器,然后我们可以访问 render
方法。
所以,我对 类 的更改如下:
在我的邮件中
def one_week_expiration(member, renewal, org)
template_environment = {
org: org,
member: member,
renewal: renewal
}
template = slim(env: template_environment) do
%q(
= content_for :header
= org.name
h3 Hello, #{member.name},
| Your membership for #{org.name} is set to expire at #{renewal.expires_at.to_s(:short)}.
br
| Please visit <a href=#{org.url}>#{org.url}</a> to purchase a membership renewal.
br
br
)
end
subject = "Your membership for #{org.name} is going to expire soon"
mail(to: member.email, subject: subject) do |format|
format.html { render text: template }
end
end
然后是重要的一点(在 application_mailer 中)
require 'render_anywhere'
class ApplicationMailer < ActionMailer::Base
layout 'email'
default from: APPLICATION_CONFIG['support_email']
def slim(env: {})
raise 'No Block Given' unless block_given?
RenderAnywhere::RenderingController.new.render_to_string(
inline: yield, layout: 'email', type: :slim, locals: env
)
end
end
就是这样。
部分原因是 React 非常方便,可以在组件中添加一小段模板代码。这是我目前所拥有的:
# app/mailers/membership_mailer.rb
# frozen_string_literal: true
class MembershipMailer < ApplicationMailer
def one_week_expiration(member, renewal, org)
template = slim(%(
= content_for :header
= org.name
Hello, #{member.name},
br
Your membership for #{org.name} is set to expire at
#{renewal.expires_at.to_s(:short)}.
br
Please visit <a href=#{org.url}>#{org.url}</a> to purchase
a membership renewal.
br
br
))
subject = "Your membership for #{org.name} is going to expire soon"
mail(to: member.email, subject: subject) do |format|
# there isn't a slim renderer
format.html { render text: template }
end
end
end
我的所有电子邮件都使用此布局,并将其设置为默认布局 application_mailer。
# app/views/layouts/email.html.slim
= stylesheet_link_tag "email"
header
.shell
h1 = yield :header
.shell
br
= yield :body
= yield
= render partial: "/shared/email_footer"
我想要这里的所有呈现方法,但我正在努力解决的问题是在哪里可以找到/弄清楚如何构建邮件程序模板、布局,并让所有我想要访问的变量都通过到模板。
# app/mailers/application_mailer.rb
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: APPLICATION_CONFIG['support_email']
layout 'email'
# taken from slim/test/block rendering / helper
def slim(source, options = {}, &block)
scope = options.delete(:scope)
locals = options.delete(:locals)
Slim::Template.new('', {}) { source }.render(scope, locals, &block)
end
end
最终,我会有 erb、arbre 等
所以,总而言之,当我调用 mail(...){...} 时,我希望能够在 ruby 中定义我的模板,而不是在模板文件中,因为我不喜欢邮件程序和模板相隔太远(在文件系统中..为什么不在这个问题的范围内,我只想解决 ruby 中的渲染问题现在)。
看起来最简单的方法是使用 render_anywhere,这只是一个非常精简的控制器,然后我们可以访问 render
方法。
所以,我对 类 的更改如下:
在我的邮件中
def one_week_expiration(member, renewal, org)
template_environment = {
org: org,
member: member,
renewal: renewal
}
template = slim(env: template_environment) do
%q(
= content_for :header
= org.name
h3 Hello, #{member.name},
| Your membership for #{org.name} is set to expire at #{renewal.expires_at.to_s(:short)}.
br
| Please visit <a href=#{org.url}>#{org.url}</a> to purchase a membership renewal.
br
br
)
end
subject = "Your membership for #{org.name} is going to expire soon"
mail(to: member.email, subject: subject) do |format|
format.html { render text: template }
end
end
然后是重要的一点(在 application_mailer 中)
require 'render_anywhere'
class ApplicationMailer < ActionMailer::Base
layout 'email'
default from: APPLICATION_CONFIG['support_email']
def slim(env: {})
raise 'No Block Given' unless block_given?
RenderAnywhere::RenderingController.new.render_to_string(
inline: yield, layout: 'email', type: :slim, locals: env
)
end
end
就是这样。