因为工作需要使用Nvelocity 所以简单的做了个小例子 首先建立一个hmtl页面 然后添加 替换的脚本如红色的部分注意$后边的字符不能事中文和独立的数字 <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd> <html xmlns= > <head> <title></title> </head> <body> $m $q </body> </html> 然后添加一个aspx 页面 首先引用 Nvelocitydll 然后 using System; using SystemCollectionsGeneric; using NVelocity; using NVelocityApp; using NVelocityRuntime; using NVelocityContext; using CommonsCollections; using SystemIO; using SystemText; namespace loginDemo { public partial class Nvelocity : SystemWebUIPage { protected void Page_Load(object sender EventArgs e) { } //这个方法用于页面合并后打印到前台页面 void MergeTemplate() { //创建NVelocity引擎的实例对象 VelocityEngine velocity = new VelocityEngine(); //初始化该实例对象 ExtendedProperties props = new ExtendedProperties(); propsAddProperty(RuntimeConstantsRESOURCE_LOADER file); propsAddProperty(RuntimeConstantsFILE_RESOURCE_LOADER_PATH PathGetDirectoryName(RequestPhysicalPath)); propsAddProperty(RuntimeConstantsINPUT_ENCODING gb); propsAddProperty(RuntimeConstantsOUTPUT_ENCODING gb); velocityInit(props); //从文件中读取模板 Template temp = velocityGetTemplate(template/); IContext context = new VelocityContext(); contextPut(m 亲); contextPut(q 李坤然); //合并模板 StringWriter writer = new StringWriter(); tempMerge(context writer); //输入 ResponseWrite(writerToString()Replace(\r\n <br/>)); } //这个页面用于再本地根据模板生成相应的 html文件cms中应该会用到 void WriterTemplate { //创建NVelocity引擎的实例对象 VelocityEngine velocity = new VelocityEngine(); //初始化该实例对象 ExtendedProperties props = new ExtendedProperties(); propsAddProperty(RuntimeConstantsRESOURCE_LOADER file); propsAddProperty(RuntimeConstantsFILE_RESOURCE_LOADER_PATH PathGetDirectoryName(RequestPhysicalPath)); propsAddProperty(RuntimeConstantsINPUT_ENCODING gb); propsAddProperty(RuntimeConstantsOUTPUT_ENCODING gb); velocityInit(props); //从文件中读取模板 Template temp = velocityGetTemplate(template/); IContext context = new VelocityContext(); contextPut(m 亲); contextPut(q 李坤然); //合并模板 StringWriter writer = new StringWriter(); tempMerge(context writer); //生成静态页 using (StreamWriter writer = new StreamWriter(ServerMapPath(/) + lfalseEncodingUTF)) { writerWrite(writer); writerFlush(); writerClose(); } } } } |