【问题标题】:Iterating through array in SendGrid email template遍历 SendGrid 电子邮件模板中的数组
【发布时间】:2016-04-02 09:47:28
【问题描述】:

我正在尝试使用 Ruby on Rails 遍历集合并在 SendGrid 模板中显示信息。

recipient = SendGrid::Recipient.new("sergio@gmail.com")
recipient.add_substitution("username", user.github_id)
recipient.add_substitution("numbers", [1,2,3,4])

在 gmail 中,此模板的格式为:

sergiotapia
ARRAY(0x85b9d90)

模板的实际代码,从 SendGrid 的编辑器中复制:

<html>
  <head>
    <title></title>
  </head>
  <body>
    <div>&lt;%body%&gt;</div>

    <div>username</div>

    <div>numbers</div>

    <p>This is a small example email.</p>
  </body>
</html>

如何遍历 SendGrid 模板中的通用数组或对象?对于这个特定示例,用户有很多 posts,我只想在 &lt;li&gt; 元素中显示用户帖子的标题。

我只是尝试用一个简单的数字数组来看看它是如何工作的。

【问题讨论】:

    标签: api email substitution sendgrid transactional-email


    【解决方案1】:

    2018 年 8 月更新:

    Sendgrid 现在使用把手从交易电子邮件中提供迭代器,更多信息请参见文档:

    https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#basic-iterator

    【讨论】:

      【解决方案2】:

      迭代数据示例:

      {
        "people":[{"name":"Bob"},{"name":"Sally"}]
      }
      

      代码:

      {{#if people}}
        <p>People:</p>
        {{#each people}}
          <p>{{this.name}}</p>
        {{/each}}
      {{/if}}
      

      结果:

      人:

      鲍勃

      莎莉

      【讨论】:

        【解决方案3】:
        {{#each data.products}}
            {{name}}: {{price}} <br/>
        {{/each}}
        
        {"data":{"products": [{"name": "Tomato", "price": "5"}, {"name": "Banana", "price": "8"}]}}
        

        【讨论】:

        • 虽然这段代码可以回答问题,但最好解释一下如何解决问题,并提供代码作为示例或参考。仅代码的答案可能会令人困惑且缺乏上下文。
        【解决方案4】:

        更新

        SendGrid 现在支持动态模板!

        您可以在他们的博客上阅读: https://sendgrid.com/blog/how-to-use-sendgrids-dynamic-templates-for-your-transactional-emails/

        旧答案:

        搜索这个结果如下GitHub issue。所以使用 SendGrid 是不可能的(还没有?)。

        但是还有其他方法可以做到这一点。使用sendwithus,您可以访问更强大的模板编辑器,该编辑器支持循环和迭代。

        只需使用您自己的 SendGrid API 密钥进行设置,您就可以使用 sendwithus 模板中的数组,该模板将使用 SendGrid 发送邮件。

        【讨论】:

          【解决方案5】:

          不幸的是,SendGrid 提供的模板目前非常少。模板不支持将数组作为值,并且没有条件或循环控件,因此您需要在构建模板和模板内容之前预先确定所有内容。一个更强大的模板系统即将推出。

          【讨论】:

          • youtube.com/watch?v=09s-c2JVI40 - 我可以发送一个大字符串并在模板中替换它吗?我可以在 Rails 中循环和渲染,然后将该块发送到模板中进行替换。那是犹太洁食吗?
          • 是的,这是个好方法,here's a similar example
          • 这个答案仍然有效还是 SenGrid 实现了循环?
          【解决方案6】:

          这是一种解决方法,Sendgrid 尚未为此更新其模板引擎

          大家好,我需要在我的 sendgrid 邮件中进行一些迭代并遇到这个问题,现在我有一个临时解决方法可以解决这个问题。这是我的解决方法

          • 创建了一个 txt 文件并在那里加载了我的 html 模板
          • 注意我想要迭代的区域已替换为 sendgrid 变量,例如 %list of items%
          • 将 txt 文件读入字符串创建字符串生成器并将所有迭代对象传递到 %list of items% 变量中

          然后通过 sendgrid 将整个字符串内容作为消息发送

          public void sendSimpleMessage(String message,
                          String subject, 
                          String toEmail,
                          String fromEmail){
                           Email from = new Email(fromEmail);
                           Email to = new Email(toEmail);
                           Content content = new Content("text/html", message);
                           Mail mail = new Mail(from, subject, to, content);
          
          
                           SendGrid sg = new SendGrid(sendgridApiKey);
                           Request request = new Request();
                           try {
                                request.method = Method.POST;
                                request.endpoint = "mail/send";
                                request.body = mail.build();
                                sg.api(request);
                           } catch (IOException ex) {
                                ex.printStackTrace();
                          }
                  }
          

          希望它可以帮助某人 https://github.com/sendgrid/sendgrid-nodejs/issues/221#issuecomment-361489007

          【讨论】:

            【解决方案7】:
                    "businessContributors" : [
                                {
                                    "total" : {
                                        "amount" : 11340,
                                        "title" : "Dhama Ji Total"
                                    },
                                    "list" : {
                                        "Dhama Ji Paid" : -296310,
                                        "HDFC Account" : 0,
                                        "Dhama Ji Received" : 307650
                                    },
                                    "title" : "Dhama Ji Owner Account"
                                },
                                {
                                    "total" : {
                                        "amount" : -1270,
                                        "title" : "Rahul Total"
                                    },
                                    "list" : {
                                        "Rahul Paid" : 243838,
                                        "Rahul Received" : 242568
                                    },
                                    "title" : "Rahul Account"
                                },
                    ]
                
                
               in email template  :-
            
             
                 <h4>Business Contributors </h4>
                        <ul>
                            {{#each businessContributors}}
                            <li> {{this.title}} <br>
                                {{#each this.list}}
                                 {{@key}} = {{this}} <br>
                            {{/each}}  </li>
                            
            
                  
                     
                            <hr style="height: 2px; background-color: black;"><br>
                            <h2>{{this.total.title}}</h2><br>
                            <h2>{{this.total.amount}}</h2>
                            <br><br>
                        {{/each}}
                        </ul>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-06-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-07-12
              相关资源
              最近更新 更多