Подтвердить что ты не робот

Разметки строк и разделов 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;
    }
}

Используется служба шаблонов, которую я использую, хотя это просто реализация по умолчанию:

    internal ITemplateService InstanceDefaultTemplateService()
    {
        ITemplateServiceConfiguration configuration = new TemplateServiceConfiguration();
        ITemplateService service = new TemplateService(configuration);
        return service;
    }

Так как в этом случае, в частности, я буду создавать электронные письма из этих шаблонов. Я хочу иметь возможность использовать @section для темы электронной почты и разных разделов тела электронной почты, используя макет, где я указываю стили, которые являются общими для всей структуры электронной почты (которая будет выглядеть как одна из MailChimp, вероятно).

Вопрос тогда двоякий:

  • Как я могу указать макеты в RazorEngine?
  • Как я могу указать эти макеты из строк (или потока)? поскольку, как вы можете видеть, я использую встроенные ресурсы для хранения шаблонов электронной почты бритвы.

Update

Возможно, я не был ясен, но я имею в виду библиотеку RazorEngine.

4b9b3361

Ответ 1

Оказывается после некоторого копирования, который поддерживает макеты, нам просто нужно объявить их с помощью _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;
        }
    }
}

Затем вы просто подключите его так:

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

Тип, который вы передаете, предназначен только для ссылки на сборку и пространство имен, в которое встроены ресурсы.

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
    {
    }
}

Наконец-то, чтобы разрешить шаблоны с помощью нашего распознавателя, мы должны разрешить их следующим образом:

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

.Run - просто простой метод расширения, так как я не могу найти никакого использования для ViewBag.

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

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

Ответ 2

Мне нужно было предоставить собственный макет как строку или имя файла. Вот как я решил это (на основе этого сообщения в блоге)

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

Затем я вызываю RazorEngineConfigurator.Configure() в Global.asax.cs и готов.

Путь к моим шаблонам находится в Properties.Settings.Default.EmailTemplatesLocation

На мой взгляд, у меня есть следующее:

@{ Layout = "_layout.html";}

_layout.html находится в электронной почтеTemplatesFolder

Это довольно стандартный HTML-код с вызовом @RenderBody() в середине.

Насколько я понимаю, RazorEngine использует имя шаблона ( "_layout.html" в этом случае) в качестве ключа к его кешу, поэтому делегат в моем конфигураторе вызывается только один раз для каждого шаблона.

Я думаю, что он использует этот резольвер для каждого имени шаблона, которого он не знает (пока).

Ответ 3

Похоже, кто-то другой решил это для вас.

https://github.com/aqueduct/Appia/blob/master/src/Aqueduct.Appia.Razor/RazorViewEngine.cs

Код, который вы хотите, находится во втором методе ExecuteView. Хотя они создают свой собственный механизм просмотра, вы можете вместо этого создать свое собственное решение для шаблонов и использовать что-то подобное. В основном они ищут свойство Layout шаблона и, если оно существует, выполняют поиск и замену содержимого из макета.

Вот ссылка на пользовательский шаблон RazorEngine:

http://razorengine.codeplex.com/wikipage?title=Building%20Custom%20Base%20Templates&referringTitle=Documentation

Вот где я нашел ваше решение:

Механизм .NET Razor - реализация макетов