Nvelocity now belongs to the castle project. Introduction: http://www.castleproject.org/others/nvelocity/index.html
Usage: http://www.castleproject.org/others/nvelocity/usingit.html
It is found that many people will encounter the unable to find resource "XXX" error when using the above method.
Solution:
Used before engine. INIT (props );
Props. addproperty ("file. Resource. loader. Path", server. mappath ("."));
The 2nd parameter server. mappath (".") indicates the directory where the current template file is located. If the complete path of your template file is: C: \ XX. VM
Replace server. mappath (".") with c: \ xx.
ExampleCode:
Myfirsttemplate. VM and nvelocitydemo. aspx are in the same directory.
1. myfirsttemplate. VM file:
From: $ from
To: $
Subject: $ subject
Hello, $ customer
2. nvelocitydemo. aspx. CS code:
Using commons. collections;
Using nvelocity;
Using nvelocity. app;
Using nvelocity. context;
Using system. IO;
Using system. text;
Namespace webtest
{
Public partial class nvelocitydemo: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Velocityengine engine = new velocityengine ();
Extendedproperties props = new extendedproperties ();
// Server. mappath (".")
Props. addproperty ("file. Resource. loader. Path", server. mappath ("."));
Engine. INIT (props );
Template template = engine. gettemplate ("myfirsttemplate. VM ");
Velocitycontext context = new velocitycontext ();
Context. Put ("from", "Somewhere ");
Context. Put ("to", "someone ");
Context. Put ("subject", "Welcome to nvelocity ");
Context. Put ("customer", "Jon Zhao ");
Stringwriter writer = new stringwriter ();
Template. Merge (context, writer );
Response. Write (writer. getstringbuilder (). tostring ());
}
}
}