RazorEngine字符串布局和部分?

bev*_*qua 19 c# embedded-resource razorengine

我像这样使用剃须刀引擎:

public class EmailService : IService
{
    private readonly ITemplateService templateService;

    public EmailService(ITemplateService templateService)
    {
        if (templateService == null)
        {
            throw new ArgumentNullException("templateService");
        }
        this.templateService = templateService;
    }

    public string GetEmailTemplate(string templateName)
    {
        if (templateName == null)
        {
            throw new ArgumentNullException("templateName");
        }
        Assembly assembly = Assembly.GetAssembly(typeof(EmailTemplate));
        Stream stream = assembly.GetManifestResourceStream(typeof(EmailTemplate), "{0}.cshtml".FormatWith(templateName));
        string template = stream.ReadFully();
        return template;
    }

    public string GetEmailBody(string templateName, object model = null)
    {
        if (templateName == null)
        {
            throw new ArgumentNullException("templateName");
        }
        string template = GetEmailTemplate(templateName);
        string emailBody = templateService.Parse(template, model, null, null);
        return emailBody;
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用的模板服务是注入的,虽然它只是一个默认实现:

    internal ITemplateService InstanceDefaultTemplateService()
    {
        ITemplateServiceConfiguration configuration = new TemplateServiceConfiguration();
        ITemplateService service = new TemplateService(configuration);
        return service;
    }
Run Code Online (Sandbox Code Playgroud)

特别是在这种情况下,我将从这些模板构建电子邮件.我希望能够使用@sections作为电子邮件的主题,以及电子邮件正文的不同部分,同时使用布局,我指定整个电子邮件结构共有的样式(看起来像MailChimp的一个)大概).

问题是双重的:

  • 如何指定布局RazorEngine
  • 如何从字符串(或流)中指定这些布局?正如您所看到的,我使用嵌入式资源来存储剃须刀电子邮件模板.

更新

也许我不清楚,但我指的是RazorEngine库.

bev*_*qua 17

经过一些挖掘支持布局后,我们只需要声明它们_Layout而不是Layout

至于嵌入式资源问题,我实现了以下内容 ITemplateResolver

using System;
using System.IO;
using System.Reflection;
using Bruttissimo.Common;
using RazorEngine.Templating;

namespace Website.Extensions.RazorEngine
{
    /// <summary>
    /// Resolves templates embedded as resources in a target assembly.
    /// </summary>
    public class EmbeddedTemplateResolver : ITemplateResolver
    {
        private readonly Assembly assembly;
        private readonly Type type;
        private readonly string templateNamespace;

        /// <summary>
        /// Specify an assembly and the template namespace manually.
        /// </summary>
        /// <param name="assembly">The assembly where the templates are embedded.</param>
        /// <param name="templateNamespace"></param>
        public EmbeddedTemplateResolver(Assembly assembly, string templateNamespace)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (templateNamespace == null)
            {
                throw new ArgumentNullException("templateNamespace");
            }
            this.assembly = assembly;
            this.templateNamespace = templateNamespace;
        }

        /// <summary>
        /// Uses a type reference to resolve the assembly and namespace where the template resources are embedded.
        /// </summary>
        /// <param name="type">The type whose namespace is used to scope the manifest resource name.</param>
        public EmbeddedTemplateResolver(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            this.assembly = Assembly.GetAssembly(type);
            this.type = type;
        }

        public string Resolve(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            Stream stream;
            if (templateNamespace == null)
            {
                stream = assembly.GetManifestResourceStream(type, "{0}.cshtml".FormatWith(name));
            }
            else
            {
                stream = assembly.GetManifestResourceStream("{0}.{1}.cshtml".FormatWith(templateNamespace, name));
            }
            if (stream == null)
            {
                throw new ArgumentException("EmbeddedResourceNotFound");
            }
            string template = stream.ReadFully();
            return template;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你就像这样连线:

    internal static ITemplateService InstanceTemplateService()
    {
        TemplateServiceConfiguration configuration = new TemplateServiceConfiguration
        {
            Resolver = new EmbeddedTemplateResolver(typeof(EmailTemplate))
        };
        ITemplateService service = new TemplateService(configuration);
        return service;
    }
Run Code Online (Sandbox Code Playgroud)

您传递的类型仅用于引用嵌入资源的程序集和命名空间.

namespace Website.Domain.Logic.Email.Template
{
    /// <summary>
    /// The purpose of this class is to expose the namespace of razor engine templates in order to
    /// avoid having to hard-code it when retrieving the templates embedded as resources.
    /// </summary>
    public sealed class EmailTemplate
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

最后一件事,为了使用我们的解析器解析模板,我们必须像这样解决它们:

ITemplate template = templateService.Resolve(templateName, model);
string body = template.Run();
return body;
Run Code Online (Sandbox Code Playgroud)

.Run只是一个简单的扩展方法,因为我找不到任何用途ViewBag.

public static class ITemplateExtensions
{
    public static string Run(this ITemplate template)
    {
        ExecuteContext context = new ExecuteContext();
        string result = template.Run(context);
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

这是缺少的扩展

    public static string FormatWith(this string text, params object[] args)
    {
        return string.Format(text, args);
    }

    public static string ReadFully(this Stream stream)
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 我们必须编写所有这些代码是荒谬的.我认为我们使用第三方库是有原因的........... (4认同)

Zar*_*dan 5

我需要以字符串或文件名的形式提供我自己的布局。这是我解决这个问题的方法(基于这篇博文

public static class RazorEngineConfigurator
{
    public static void Configure()
    {
        var templateConfig = new TemplateServiceConfiguration
            {
                Resolver = new DelegateTemplateResolver(name =>
                    {
                        //no caching cause RazorEngine handles that itself
                        var emailsTemplatesFolder = HttpContext.Current.Server.MapPath(Properties.Settings.Default.EmailTemplatesLocation);
                        var templatePath = Path.Combine(emailsTemplatesFolder, name);
                        using (var reader = new StreamReader(templatePath)) // let it throw if doesn't exist
                        {
                            return reader.ReadToEnd();
                        }
                    })
            };
        RazorEngine.Razor.SetTemplateService(new TemplateService(templateConfig));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我在 Global.asax.cs 中调用 RazorEngineConfigurator.Configure() 就可以了。

我的模板的路径在 Properties.Settings.Default.EmailTemplatesLocation

在我看来,我有这个:

@{ Layout = "_layout.html";}
Run Code Online (Sandbox Code Playgroud)

_layout.html 在 emailsTemplatesFolder 中

这是一个非常标准的 HTML,中间有一个 @RenderBody() 调用。

据我了解,RazorEngine 使用模板名称(在本例中为“_layout.html”)作为其缓存的键,因此我的配置器中的委托每个模板只调用一次。

我认为它对每个它不知道的模板名称使用该解析器(还)。