果园cms渲染形状作为电子邮件模板 [英] orchard cms rendering shape as email template

查看:121
本文介绍了果园cms渲染形状作为电子邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将形状呈现为电子邮件模板。我希望能够从后台任务以及从当前请求中执行此操作。有人有任何技巧给我吗?我认为Orchard.DisplayManagement.IDisplayHelperFactory是关键,但是后来我需要制作ViewContext和IViewDataContainer,我可以从Orchard.Mvc.ViewEngines.Razor.WebViewPage中获得。

I am trying to render shapes as email templates. I would like to be able to do this from a background task as well as from the current request. Does anyone have any tips for me? I think the Orchard.DisplayManagement.IDisplayHelperFactory is the key, but then I need to manufacture the ViewContext and IViewDataContainer, which I could possibly get from the Orchard.Mvc.ViewEngines.Razor.WebViewPage perhaps?

任何人做了类似的事情?我正在查看 https://github.com/andrewdavey/postal /blob/master/src/Postal/EmailViewRenderer.cs 有一些灵感,只是想知道有没有人可以把我放在正确的轨道上?

Anyone done anything similar? I am looking at https://github.com/andrewdavey/postal/blob/master/src/Postal/EmailViewRenderer.cs for some inspiration, just wondering if anyone can put me on the right track?

再次感谢提前!

推荐答案

在通过果园来源挖掘一下,并从Andrew Davey的邮政应用中获得了一些启发,我设法提出有一个解决方案。查看代码片段

After a bit of digging through the Orchard source and some inspiration taken from Andrew Davey's postal application I managed to come up with a solution. See code fragment below

private void RenderMessage(MessageContext context, dynamic shape)
{
        var httpContext = new EmailHttpContext(new Uri("http://localhost/orchard/"));            
        var routeData = new RouteData();
        routeData.DataTokens.Add("IWorkContextAccessor", _workContextAccessor);
        routeData.Values["controller"] = "Dummy";
        var requestContext = new RequestContext(httpContext, routeData);
        var controllerContext = new ControllerContext(requestContext, new DummyController());
        var viewContext = new ViewContext(controllerContext, new ShapeView(shape), new ViewDataDictionary(shape.Model), new TempDataDictionary(), new StringWriter());
        var scope = _workContextAccessor.CreateWorkContextScope(viewContext.HttpContext);
        scope.WorkContext.CurrentTheme = _siteThemeService.GetSiteTheme();            
        var page = new EmailWebViewPage(viewContext, new ViewDataDictionary<dynamic>(shape.Model));

        var displayHelperFactory = _services.WorkContext.Resolve<IDisplayHelperFactory>();            
        var display = displayHelperFactory.CreateHelper(page.ViewContext, page);
        context.MailMessage.Body = display(shape).ToHtmlString();
        scope.Dispose();
    }

    class DummyController : Controller
    {
    }

    class ShapeView : IView
    {
        private readonly dynamic _shape;

        public ShapeView(dynamic shape)
        {
            _shape = shape;
        }

        #region IView Members

        public void Render(ViewContext viewContext, TextWriter writer)
        {
        }

        #endregion
    }

EmailHttpContext是从邮政项目中获取的。这被用来构造控制器和查看上下文。然后,我只需要扩展果园的WebViewPage来创建EmailWebViewPage,使我能够利用果园基础设施访问视图引擎等。

The EmailHttpContext was taken from the postal project. This was used to constructu the controller and view contexts. I then just had to extend orchard's WebViewPage to create the EmailWebViewPage that enables me to tap into the orchard infrastructure to access view engines etc.

这不是最漂亮的解决方案,需要要彻底测试,但似乎做了我所追求的。

It's not the prettiest solution, and needs to be thoroughly tested, but appears to do what I am after.

如果有任何人有一些反馈意见,或者想要完整的代码,请随意放下一条线。

If anyone has some feedback, or would like the complete code, feel free to drop me a line.

这篇关于果园cms渲染形状作为电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆