我正在使用剃刀引擎https://github.com/Antaris/RazorEngine来解析我的电子邮件模板的正文。是否可以定义布局并包含其他.cshtml文件?例如公共页眉和页脚。
发布于 2012-08-03 23:39:09
在这两篇文章的帮助下,我得到了通用的模板和布局:
RazorEngine string layouts and sections?
http://blogs.msdn.com/b/hongyes/archive/2012/03/12/using-razor-template-engine-in-web-api-self-host-application.aspx
这是我的解决方案:
解决方案1:Layout
由设置_Layout使用
@{
_Layout = "Layout.cshtml";
ViewBag.Title = Model.Title;
}页脚
@section Footer
{
@RenderPart("Footer.cshtml")
}Layout.cshtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
<head>
</head>
<body>
<div id="content">
@RenderBody()
</div>
@if (IsSectionDefined("Footer"))
{
<div id="footer">
@RenderSection("Footer")
</div>
}
</body>
</html>TemplateBaseExtensions
使用RenderPart方法扩展TemplateBase
public abstract class TemplateBaseExtensions<T> : TemplateBase<T>
{
public string RenderPart(string templateName, object model = null)
{
string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", templateName);
return Razor.Parse(File.ReadAllText(path), model);
}
}剃刀配置
将BaseTemplateType设置为TemplateBaseExtensions类
TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
{
BaseTemplateType = typeof(TemplateBaseExtensions<>)
};
Razor.SetTemplateService(new TemplateService(templateConfig));编辑解决方案2:
如果您使用的是TemplateResolver。不需要RenderPart,请使用@Include
页脚
@section Footer
{
@Include("Footer.cshtml")
}解析器
public class TemplateResolver : ITemplateResolver
{
public string Resolve(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
string path = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Templates", name);
return File.ReadAllText(path, System.Text.Encoding.Default);
}
}配置
TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
{
Resolver = new TemplateResolver()
};
Razor.SetTemplateService(new TemplateService(templateConfig));松饼人的更新指定模板并呈现字符串
var templateResolver = Razor.Resolve("Registration.cshtml");
return templateResolver.Run(new ExecuteContext());此外,我和这个链接上的其他人在使用_Layout时遇到了问题,而Layout工作得很好。
'_Layout‘是旧的语法。在未来的版本中,它被更新为“布局”。
发布于 2012-07-10 21:59:20
你可以很容易地用Razor做很多事情;然而,这个特定的项目似乎抽象出了你可以做的很多Razor引擎的东西(这有好的也有坏的)。在你的情况下,听起来你最好实现你自己的Razor解决方案(实际上并不是那么糟糕),然后你可以让你的模板抛出异常或者很容易地引入其他内容。
例如,滚动您自己的解决方案允许您为您的剃刀模板创建一个基类,该基类可以通过调用其他模板来公开拉入“局部视图”的能力。此外,如果某些属性为空,则可以执行模型检查并抛出异常。
发布于 2016-10-03 02:24:56
使用RazorEngine实现布局的最简单方法是在布局的@RenderBody()中替换模板返回的内容:
var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);例如:
您的_Layout.cshtml带有典型的@RenderBody()
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
<head>
</head>
<body>
<div>
@RenderBody()
</div>
</body>
</html>您的RazorEngine模板MyTemplate.cshtml
@using RazorEngine.Templating
@inherits TemplateBase<myviewmodel>
<h1>Hello People</h1>
<p>@Model</p>无论您在何处调用RazorEngine模板:
var TemplateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates");
var template = File.ReadAllText(Path.Combine(TemplateFolderPath,"MyTemplate.cshtml"));
var layout = File.ReadAllText(Path.Combine(TemplateFolderPath, "_Layout.cshtml"));
var templateService = new TemplateService();
var templateHtml = templateService.Parse(template, myModel, null, null);
var finalHtml = layout.Replace(@"@RenderBody()", templateHtml);https://stackoverflow.com/questions/11414194
复制相似问题