NVelocity ASP.NET 示例
我希望在 ASP.NET MVC 应用程序中使用 NVelocity,而不是作为视图引擎,仅用于呈现一些电子邮件模板。
然而,我一生都无法让它发挥作用。我已经从 castle 项目下载了它,并按照 http://www 中的示例进行操作.castleproject.org/others/nvelocity/usingit.html#step1
无论我尝试什么,我似乎都无法加载位于我的网站中的模板。该示例建议使用绝对路径,我已尝试过但无济于事:
Template t = engine.GetTemplate("/Templates/TestEmail.vm");
所以请有人给我两个例子。一是加载位于网站目录中的模板,二是解析字符串变量(因为我的模板可能会存储在数据库中)。
非常感谢 本
I'm looking to use NVelocity in my ASP.NET MVC application, not as a view engine, just for rendering some email templates.
However, I cannot for the life of me get it to work. I have downloaded it from the castle project and followed the example at http://www.castleproject.org/others/nvelocity/usingit.html#step1
No matter what I try I don't seem to be able to load a template located in my site. The example suggests using the absolute path, which I have tried to no avail:
Template t = engine.GetTemplate("/Templates/TestEmail.vm");
So please can someone give me two examples. One of loading a template located in the web site directory and secondly one parsing a string variable (as it is likely that my templates will be stored in a database).
Many thanks
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在过去的一个项目中使用过这个类:
为了实例化 NVelocityTemplateRepository 类,您需要提供模板根目录所在的绝对路径。然后使用相对路径来引用
vm
文件。I've used this class in one of my past projects:
In order to instantiate the
NVelocityTemplateRepository
class you need to provide an absolute path where your templates root is. Then you use relative paths to reference yourvm
files.我还添加了以下方法来处理字符串而不是模板文件(例如,如果从数据库检索模板内容):
并使用 StructureMap 来初始化服务:
I also added the following method to process a string instead of a template file (say if retrieving the template content from a database):
And used StructureMap to initialize the service:
您可能会发现 TemplateEngine 组件很有用。
它是对带有 NVelocity 实现的模板引擎的抽象,类似于 Darin 的回答,但它的性能应该会稍微好一些,因为它使用 VelocityEngine 的单个实例(而不是每次渲染初始化一个实例)并且具有可选的缓存。它还具有其他一些功能,例如日志记录、NVelocity 属性覆盖和从程序集资源加载模板。
You might find the TemplateEngine component useful.
It's an abstraction over template engines with a NVelocity implementation, similar to Darin's answer, but it should perform marginally better since it uses a single instance of the VelocityEngine (as opposed to initializing one instance per render) and has optional caching. It also has a couple other features, like logging, NVelocity property overriding and loading templates from assembly resources.