mandrill中的每个循环使用把手和Mandrill-dotnet -library

时间:2015-03-30 19:55:53

标签: c# json templates mandrill

我正在使用Mandrill发送电子邮件,并使用Handlebars在电子邮件中呈现内容。

如果我添加这样的变量,一切正常:

后端:

message.AddRecipientVariable("test@gmail.com", "MYVALUE", "some value");

HTML的模板:

<p>{{MYVALUE}}</p>

但如果我尝试使用{{#each}} - 发送的电子邮件中的每个部分都是空的。我在这做错了什么。我是否以错误的方式传递objStringArray或者是否有其他错过的东西来渲染循环?

后端:

var objList = new List<MyObj> {new MyObj() {Qty = "125"}, new MyObj() { Qty = "16"}};

var jsonSerialiser = new JavaScriptSerializer();
var objStringArray = jsonSerialiser.Serialize(objList.ToArray());

message.AddRecipientVariable("test@gmail.com", "VALUES", objStringArray);

message.merge_language = "handlebars";
_mandrillApi.SendMessage(message, templateName, new List<TemplateContent>());

HTML的模板:

<ul>
  {{#each VALUES}}
    <li>{{Qty}}</li>
  {{/each}}
</ul>

Handlebars in mandrill

Mandrill-dotnet

2 个答案:

答案 0 :(得分:2)

根据我正在使用的库(Mandrill-DotNet)方法:AddRecipientVariable()仅将类型字符串作为内容变量。并且madrill-API在执行{{#each}}时需要一个列表。因此,通过稍微更改库源代码,我可以将常规List<dynamic>()传递给AddRecipientVariable - s内容参数,而且它可以完美运行。

通过更改源代码:

public struct merge_var
{
    #region Fields

    /// <summary>
    ///     The content.
    /// </summary>
    public string content;

    /// <summary>
    ///     The name.
    /// </summary>
    public string name;

    #endregion
}

    public void AddRecipientVariable(string recipient, string name, string content)
    {
        if (this.merge_vars == null)
        {
            this.merge_vars = new List<rcpt_merge_var>();
        }

        rcpt_merge_var entry = this.merge_vars.Where(e => e.rcpt == recipient).FirstOrDefault();
        if (entry == null)
        {
            entry = new rcpt_merge_var { rcpt = recipient };
            this.merge_vars.Add(entry);
        }

        var mv = new merge_var { name = name, content = content };

        entry.vars.Add(mv);
    }

到此(string contentdynamic content):

public struct merge_var
{
    #region Fields

    /// <summary>
    ///     The content.
    /// </summary>
    public dynamic content;

    /// <summary>
    ///     The name.
    /// </summary>
    public string name;

    #endregion
}

    public void AddRecipientVariable(string recipient, string name, dynamic content)
    {
        if (this.merge_vars == null)
        {
            this.merge_vars = new List<rcpt_merge_var>();
        }

        rcpt_merge_var entry = this.merge_vars.Where(e => e.rcpt == recipient).FirstOrDefault();
        if (entry == null)
        {
            entry = new rcpt_merge_var { rcpt = recipient };
            this.merge_vars.Add(entry);
        }

        var mv = new merge_var { name = name, content = content };

        entry.vars.Add(mv);
    }

答案 1 :(得分:0)

我在Python中使用Djrill,所以我不熟悉您正在使用的库。

但是你应该确保你的JSON没有以{{#each ... }} on Mandrill is resulting in an empty string

中提到的错误方式嵌套