ASP.NETMVCでNHamlViewEngineを使用するのが好きな人をたくさん見ましたが、NHamlを.NETの汎用テンプレートエンジンとして使用できるかどうか疑問に思っています。ASP MVC View Engine環境の外部で、コンソールアプリケーションからNHamlを使用するか、HTMLメールテンプレートを生成したいと思います。これは可能ですか?これを行う方法を示すコード例はどこにも見つかりませんでした。ありがとう!
2 に答える
4
はい、ASP.Net MVC なしで使用できます。私は自分の Web サーバーに使用しています (ただし、Web サーバーで使用する必要があるという意味ではありません)。
ここで使用方法を確認してください: http://webserver.codeplex.com/SourceControl/changeset/view/50874#671672
要するに、次のようなことです。
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);
于 2010-12-08T06:33:50.033 に答える
0
最新の NHaml はそれをより簡単にします:
var te = XmlConfigurator.GetTemplateEngine("nhaml.config");
var ct = te.GetCompiledTemplate(new FileViewSource(new FileInfo("period.nhaml")), typeof(Template));
var template = ct.CreateTemplate();
var viewData = new Dictionary<string,object>();
template.ViewData = viewData;
template.Render(writer);
于 2015-06-24T02:56:41.863 に答える