本文介绍了可NHaml作为一个通用的模板引擎? (MVC外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过不少,像使用NHaml视图引擎在ASP.NET MVC中的人,但我不知道是否NHaml可以作为在.NET中的通用模板引擎?我想使用NHaml从一个控制台应用程序,或生成HTML电子邮件模板,ASP的MVC视图引擎环境之外。这可能吗?我还没有发现有很多code随时随地的例子说明如何做到这一点。谢谢!

I've seen a lot of people that like using the NHaml View Engine in ASP.NET MVC, but I'm wondering if NHaml can be used as a general purpose templating engine in .NET ? I'd like to use NHaml from a Console application, or to generate HTML email templates, outside of the ASP MVC View Engine environment. Is this possible? I haven't found many code examples anywhere showing how to do this. Thanks!

推荐答案

是的,它可以在不ASP.Net MVC使用。我用它自己的Web服务器(但是,这并不意味着你必须使用它与网络服务器)。

Yes, it can be used without ASP.Net MVC. I use it for my own web server (but that doesn't mean that you HAVE to use it with web servers).

看看我是如何用在这里:的http:// Web服务器。codeplex.com / SourceControl /变更/查看/ 50874#671672

Check out how I use it here: http://webserver.codeplex.com/SourceControl/changeset/view/50874#671672

你在短期做的是这样的:

What you do in short is something like this:

TemplateEngine _templateEngine = new TemplateEngine();

// Add a type used in the template. Needed to that nhaml can find it when compiling the template
_templateEngine.Options.AddReferences(typeof (TypeInYourAssembly));

// base class for all templates
_templateEngine.Options.TemplateBaseType = typeof (BaseClassForTemplates);

//class providing content to the engine, should implement ITemplateContentProvider
_templateEngine.Options.TemplateContentProvider = this; 

// compile the template, 
CompiledTemplate template = _templateEngine.Compile(new List<string> {layoutName, viewPath},
                                                                typeof (TemplateImplementation));

//create a instance
var instance = (NHamlView)template.CreateInstance();

// provide the view data used by the template
instance.ViewData = viewData;

// render it into a text writer
instance.Render(writer);

这篇关于可NHaml作为一个通用的模板引擎? (MVC外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:46