RazorEngine布局 [英] RazorEngine layouts
问题描述
我现在用的剃刀引擎 https://github.com/Antaris/RazorEngine 解析我的电子邮件模板身体。是否有可能定义布局,包括其他的.cshtml文件?例如常见的页眉和页脚。
我有共同的模板和布局工作,这两个职位的帮助
这是我的解决方案:
解决方案1:
布局
通过设置_layout
<$ p用$ p>
@ {
_layout =Layout.cshtml;
ViewBag.Title = Model.Title;
}
页脚
@section页脚
{
@RenderPart(Footer.cshtml)
}
Layout.cshtml
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.0过渡// ENGT&; < HTML和GT;
< HEAD>
< /头>
<身体GT;
< DIV ID =内容>
@RenderBody()
< / DIV>
@if(IsSectionDefined(页脚))
{
< DIV ID =页脚>
@RenderSection(页脚)
< / DIV>
}
< /身体GT;
< / HTML>
TemplateBaseExtensions
扩展TemplateBase与RenderPart法
公共抽象类TemplateBaseExtensions< T> :TemplateBase< T>
{
公共字符串RenderPart(字符串TEMPLATENAME,对象模型= NULL)
{
路径字符串= Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,模板,TEMPLATENAME );
返回Razor.Parse(File.ReadAllText(路径),模型);
}
}
剃刀配置
设置BaseTemplateType您TemplateBaseExtensions类
TemplateServiceConfiguration templateConfig =新TemplateServiceConfiguration
{
BaseTemplateType = typeof运算(TemplateBaseExtensions<>)
};
Razor.SetTemplateService(新TemplateService(templateConfig));
修改解决方案2:
如果您使用的是TemplateResolver。 RenderPart不需要使用@include而不是
页脚
@section页脚
{
@include(Footer.cshtml)
}
旋
公共类TemplateResolver:ITemplateResolver
{
公共字符串解析(字符串名称)
{
如果(名称== NULL)
{
抛出新的ArgumentNullException(名称);
}
路径字符串= Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,模板,名);
返回File.ReadAllText(路径,System.Text.Encoding.Default);
}
}
配置
TemplateServiceConfiguration templateConfig =新TemplateServiceConfiguration
{
=解析器新TemplateResolver()
};
Razor.SetTemplateService(新TemplateService(templateConfig));
按松饼人更新
指定的模板和渲染字符串
VAR templateResolver = Razor.Resolve(Registration.cshtml);
返回templateResolver.Run(新ExecuteContext());
另外我,跟着别人在这个环节的https://github.com/Antaris/RazorEngine/issues/61 有问题,使用 _layout
,而布局
工作
'_布局是旧的语法。它在将来的版本更新为布局。
I am using the Razor engine https://github.com/Antaris/RazorEngine to parse the body of my email templates. Is it possible to define a layout and include other .cshtml files? for example a common header and a footer.
I got common templates and a layout working, with the help of these two posts:
RazorEngine string layouts and sections?
This is my solution:
Solution 1: Layout
Used by setting _Layout
@{
_Layout = "Layout.cshtml";
ViewBag.Title = Model.Title;
}
Footer
@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
Extend TemplateBase with a RenderPart Method
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);
}
}
Razor Config
Set BaseTemplateType to your TemplateBaseExtensions class
TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
{
BaseTemplateType = typeof(TemplateBaseExtensions<>)
};
Razor.SetTemplateService(new TemplateService(templateConfig));
Edit Solution 2:
If you are using a TemplateResolver. RenderPart isn't needed use the @Include instead
Footer
@section Footer
{
@Include("Footer.cshtml")
}
Resolver
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);
}
}
Config
TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration
{
Resolver = new TemplateResolver()
};
Razor.SetTemplateService(new TemplateService(templateConfig));
Update by The Muffin Man Specify a template and render a string
var templateResolver = Razor.Resolve("Registration.cshtml");
return templateResolver.Run(new ExecuteContext());
Also I, along with others at this link https://github.com/Antaris/RazorEngine/issues/61 had issues with using _Layout
whereas Layout
worked.
'_Layout' is the old syntax. It was updated to 'Layout' in a future release.
这篇关于RazorEngine布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!