我从 Orchard Token Providers 那里得到了一些帮助,任何帮助都是一件好事。我大部分时间都在复制评论令牌。
目标是在发布“问题”内容项时发送电子邮件。
这是我对内容类型“问题”的解决方案:
public interface ITokenProvider : IEventHandler
{
void Describe(dynamic context);
void Evaluate(dynamic context);
}
public class QuestionTokens : ITokenProvider
{
public QuestionTokens()
{
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(dynamic context)
{
//presume this is correct
context.For("Content", T("Content Items"), T("Content Items"))
.Token("QuestionText", T("Question Text"), T("Text of the question"))
.Token("QuestionAuthor", T("Question Author"), T("Author of the question"));
//presume this is incorrect? correct for the content type?
context.For("Question", T("Questions"), T("Questions from users"))
.Token("QuestionText", T("Question Text"), T("Text of the question"))
.Token("QuestionAuthor", T("Question Author"), T("Author of the question"));
}
public void Evaluate(dynamic context)
{
Func<IContent, object> questionTextAccessorFromContent = (content) => {
var part = content.As<QuestionPart>();
return part.QuestionText;
};
Func<QuestionPart, object> questionTextAccessor = (part) =>
{
return part.QuestionText;
};
Func<IContent, object> authorAccessorFromContent = (content) => {
var part = content.As<QuestionPart>();
return part.Author;
};
Func<QuestionPart, object> authorAccessor = (part) =>
{
return part.Author;
};
//doesnt work
context.For<IContent>("Content")
.Token("QuestionText", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.QuestionText))
.Token("QuestionAuthor", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.Author));
//doesnt work
context.For<IContent>("Question")
.Token("QuestionText", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.QuestionText))
.Token("QuestionAuthor", (Func<IContent, object>)
(content => content.As<QuestionPart>().Record.Author));
//doesnt work
context.For<QuestionPart>("Question")
.Token("QuestionText", (Func<QuestionPart, object>)
(content => content.Record.QuestionText))
.Token("Author", (Func<QuestionPart, object>)
(content => content.Record.Author)); ;
}
private static string QuestionText(IContent question)
{
var questionPart = question.As<QuestionPart>();
return questionPart.QuestionText;
}
private static string Author(IContent question)
{
var questionPart = question.As<QuestionPart>();
return questionPart.Author;
}
}
这是操作(当问题发布时),它会发送一封带有正文的电子邮件:(已更新以测试 Medeiros 建议)
<p>A question has been created by: {Content.QuestionAuthor}</p>
<p>A question has been created by: {Content.QuestionAuthor.Text}</p>
<p>A question has been created by: {Content.QuestionAuthor.Value}</p>
<p>A question has been created by: {Question.QuestionAuthor}</p>
<p>A question has been created by: {Question.QuestionAuthor.Text}</p>
<p>A question has been created by: {Question.QuestionAuthor.Value}</p>
<p>Message: {Content.QuestionText}</p>
<p>Message: {Content.QuestionText.Text}</p>
<p>Message: {Content.QuestionText.Value}</p>
<p>Message: {Question.QuestionText}</p>
<p>Message: {Question.QuestionText.Text}</p>
<p>Message: {Question.QuestionText.Value}</p>
所有“我的”标记都替换为空白文本。其他令牌,如: {Content.ContentType} {User.Email} 工作得很好。任何人注意到的任何错误或提示都将非常有用。
谢谢,马特