gpt4 book ai didi

c# - 使用 Handlebars 和 Mandrill-dotnet -library 的 Mandrill 中的每个循环

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:40 27 4
gpt4 key购买 nike

我使用 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

最佳答案

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

通过更改源代码:

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);
}

关于c# - 使用 Handlebars 和 Mandrill-dotnet -library 的 Mandrill 中的每个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29355106/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com