ASP.NET MVC에서 NHaml View Engine을 사용하는 것과 같은 사람들을 많이 보았지만 NHaml을 .NET의 범용 템플릿 엔진으로 사용할 수 있는지 궁금합니다. 콘솔 응용 프로그램에서 NHaml을 사용하거나 ASP MVC View Engine 환경 외부에서 HTML 전자 메일 템플릿을 생성하고 싶습니다. 이것이 가능한가? 나는 이것을 수행하는 방법을 보여주는 많은 코드 예제를 발견하지 못했다. 감사!NHaml을 범용 템플릿 엔진으로 사용할 수 있습니까? (MVC 외부)
4
A
답변
4
예, ASP.Net MVC없이 사용할 수 있습니다. 내 자신의 웹 서버에 사용하지만 (그렇다고 웹 서버와 함께 사용해야 함을 의미하지는 않습니다).
는체크 아웃 내가 여기를 사용하는 방법 : 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);
+0
왜 투표가 늦습니까? – jgauffin
+0
아무 생각이 없지만 요점을 알려 주셔서 저에게 1+을 주셨습니다. :-) –
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);
분명히 면도기 새로운 MVC 3의 뷰 엔진은 사용자가 설명하는 바로는 ASP.NET없이 독립형으로 사용할 수 있습니다. 따라서 NHaml이 ASP.NET 런타임과 얼마나 느슨하게 결합되었는지에 달려 있습니다. – GiddyUpHorsey
내 대답이 도움이되지 않으면 질문을 정교하게 작성하십시오. – jgauffin