【问题标题】:Is there a RAILS way to DRY up mailer Actions/Views?有没有一种 RAILS 方法来干掉邮件操作/视图?
【发布时间】:2015-04-18 17:05:22
【问题描述】:

我刚刚在我的 rails 应用程序上设置了邮件和操作/视图以及 HAML gem。我也在使用 mailgun 提供的transactional email templates。 HAML 中的典型邮件模板如下所示:

!!!  
%html{xmlns: "http://www.w3.org/1999/xhtml"}
 %head
  %meta{content: "width=device-width", name: "viewport"}/
  %meta{content: "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
  %title Alerts e.g. approaching your limit
  %link{href: "styles.css", media: "all", rel: "stylesheet", type: "text/css"}/


%body
  %table.body-wrap
    %tr
      %td
      %td.container{width: "600"}
        .content
          %table.main{cellpadding: "0", cellspacing: "0", width: "100%"}
            %tr
              %td.alert.alert-warning
                Warning: You're approaching your limit. Please upgrade.
            %tr
              %td.content-wrap
                %table{cellpadding: "0", cellspacing: "0", width: "100%"}
                  %tr
                    %td.content-block
                      You have
                      %strong 1 free report
                      remaining.
                  %tr
                    %td.content-block
                      Add your credit card now to upgrade your account to a premium plan to ensure you don't miss out on any reports.
                  %tr
                    %td.content-block
                      %a.btn-primary{href: "http://www.mailgun.com"} Upgrade my account
                  %tr
                    %td.content-block
                      Thanks for choosing Acme Inc.
          .footer
            %table{width: "100%"}
              %tr
                %td.aligncenter.content-block
                  %a{href: "http://www.mailgun.com"} Unsubscribe
                  from these alerts.
      %td

如您所见,这将是相当多的代码行,其中包含 %html、%head、%tables、%tr、%td 等。在我将在我的应用程序中使用的每个邮件模板上重复(电子邮件确认、password_reset、other_notifications、weekly_digests 等)。

我的问题:有没有一种不错的 RAILS 方法可以将所有模板代码干枯成这样:

!!!  
  = render :partial => 'widgets/mail/head'
  %body
    = render :partial => "widgets/mail/header" # <Mail header>

    = yield_mail_content # <Main tag for content>

    = render :partial => "widgets/mail/footer" # <Mail footer>

或者更好,写registration_confirmation.html.haml如下:

= render :partial => 'widgets/mail/html_above'
= render <!-- registration_mail_specific_content_here -->
= render :partial => 'widgets/mail/html_below'

编辑:使用部分它可以工作,但仍然必须维护 %tables、%tr、%td 等的 HAML 嵌套。像这样:

      .content
        %table.main{cellpadding: "0", cellspacing: "0", width: "100%"}
          = render :partial => 'widgets/mail/header'
          %tr
            %td.content-wrap
              %table{cellpadding: "0", cellspacing: "0", width: "100%"}
                %tr
                  %td.content-block
                    Hi
                    = succeed ',' do 
                      = @user.first_name.capitalize

这主要意味着 %table、%tr、%td 将继续像往常一样保留在每个邮件模板上。

【问题讨论】:

  • 我相信你可以对重复的部分使用部分?
  • 部分工作了吗?
  • 我讨厌查看电子邮件。桌子让我的眼球爆炸。 :P
  • 当我在电子邮件客户端上看到完全不同的、出乎意料的和损坏的(有时是奇怪的)输出时,我的眼睛会爆炸。 :-/
  • 请参阅stackoverflow.com/questions/12700699/…,这是问题的答案

标签: ruby-on-rails email ruby-on-rails-4 actionmailer


【解决方案1】:

我将尝试描述我当前的邮件视图解决方案。如果您有更好的想法,请告诉我。

所有示例代码都将写入haml。 邮寄者的名字是MainMailer。 邮件操作的名称是confirmation

说明:

  1. app/views/layouts/ 中为您的邮件创建布局模板,例如。 app/views/layouts/main_mailer.html.haml

  2. 对所有邮件操作共享的元素使用像headerfooter 等部分。这些部分必须放在app/views/mailer_name/ 目录中,例如。 app/views/main_mailer/

  3. 如需进一步干燥,请使用yield :section_name,例如。 yield :top。将邮件操作模板中的已生成部分定义为 content_for :section_name do 块。 (提示:不要忘记将yield放在布局基本模板中的任何位置,否则不会显示生成的部分)

此解决方案的优点在于,对于新的邮件操作,只需定义一个包含 yield 部分的文件(例如 app/views/main_mailer/confirmation.html.haml)就足够了。另一方面,对于新的邮件模型,您需要再次定义所有内容(可以在邮件模型中使用layout 并在那里指定共享布局,但我没有对此进行测试)。

示例:

app/views/main_mailer/confirmation.html.haml

- content_for :top do
  %h1 Some top section text

- content_for :content do
  %p.lead Mail content text
  %p More of the mail content

app/views/layouts/main_mailer.html.haml

-# Main template for all e-mails (like application.html.haml is for views)
!!!
%html{:xmlns => "http://www.w3.org/1999/xhtml"}
  %head
    %meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/
    %meta{:content => "width=device-width", :name => "viewport"}/
    = stylesheet_link_tag 'email', media: 'all'
  %body
    %table.body
      %tbody
        %tr
          %td.center{:align => "center", :valign => "top"}
            %center
              = render 'header'
              -# anywhere use yield in order to use yield :sth blocks
              = yield

              %table.container
                %tbody
                  %tr
                    %td
                      %table.row
                        %tbody
                          %tr
                            %td.wrapper.last
                              %table.twelve.columns
                                %tbody
                                  %tr
                                    %td= yield :top
                                    %td.expander
                      %table.row
                        %tbody
                          %tr
                            %td.wrapper.last
                              %table.twelve.columns
                                %tbody
                                  %tr
                                    %td
                                      %br
                                      = yield :content
                                    %td.expander

app/views/main_mailer/_header.html.haml

%table.row.header
  %tbody
    %tr
      %td.center{:align => "center"}
        %center
          %table.container
            %tbody
              %tr
                %td.wrapper.last
                  %table.twelve.columns
                    %tbody
                      %tr
                        %td.six.sub-columns.title-area
                          %h4= link_to @user.service.name, root_url
                        %td.six.sub-columns.last{:style => "text-align:right; vertical-align:middle;"}
                        %td.expander

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2016-09-09
    相关资源
    最近更新 更多