当前位置: 首页>>代码示例>>C#>>正文


C# VelocityEngine.GetTemplate方法代码示例

本文整理汇总了C#中NVelocity.App.VelocityEngine.GetTemplate方法的典型用法代码示例。如果您正苦于以下问题:C# VelocityEngine.GetTemplate方法的具体用法?C# VelocityEngine.GetTemplate怎么用?C# VelocityEngine.GetTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NVelocity.App.VelocityEngine的用法示例。


在下文中一共展示了VelocityEngine.GetTemplate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ProcessRequest

        public void ProcessRequest(HttpContext context)
        {
            DataBooks book=new DataBooks();
            book.name = context.Request["bookname"];
            book.type = context.Request["booktype"];
            if(book.name!=null)
                bookcollector.Add(book);

            context.Response.ContentType = "text/html";
            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
            vltEngine.Init();

            VelocityContext vltContext = new VelocityContext();

            //vltContext.Put("msg", "");
            vltContext.Put("bookcollector", bookcollector);
            vltContext.Put("book", book);

            Template vltTemplate = vltEngine.GetTemplate("Front/ShopingCar.html");//模版文件所在位置

            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            string html = vltWriter.GetStringBuilder().ToString();
            context.Response.Write(html);
        }
开发者ID:ujsxn,项目名称:UJSBookStore,代码行数:27,代码来源:ShopingCar.ashx.cs

示例2: RenderHtml

        /// <summary>
        /// 用data数据填充templateName模板,渲染生成html返回
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string RenderHtml(string templateName, object data)
        {
            //第一步:Creating a VelocityEngine也就是创建一个VelocityEngine的实例
            VelocityEngine vltEngine = new VelocityEngine();   //也可以使用带参构造函数直接实例
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
            vltEngine.Init();
            //vltEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
            //vltEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");

            //第二步:Creating the Template加载模板文件
            //这时通过的是Template类,并使用VelocityEngine的GetTemplate方法加载模板
            Template vltTemplate = vltEngine.GetTemplate(templateName);

            //第三步:Merging the template整合模板
            VelocityContext vltContext = new VelocityContext();
            vltContext.Put("Data", data);//设置参数,在模板中可以通过$data来引用

            //第四步:创建一个IO流来输出模板内容推荐使用StringWriter(因为template中以string形式存放)
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);

            string html = vltWriter.GetStringBuilder().ToString();
            return html;
        }
开发者ID:zhangtaoxgu,项目名称:aspnetProject,代码行数:31,代码来源:CommonHelper.cs

示例3: ProcessRequest

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            string username = context.Request.Form["username"];
            string password = context.Request.Form["password"];

            if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
            {

                VelocityEngine vltEngine = new VelocityEngine();
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
                vltEngine.Init();

                VelocityContext vltContext = new VelocityContext();
                //vltContext.Put("p", person);//设置参数,在模板中可以通过$data来引用
                vltContext.Put("username", "");
                vltContext.Put("password", "");
                vltContext.Put("msg", "");

                Template vltTemplate = vltEngine.GetTemplate("Front/login.html");//模版文件所在位置

                System.IO.StringWriter vltWriter = new System.IO.StringWriter();
                vltTemplate.Merge(vltContext, vltWriter);

                string html = vltWriter.GetStringBuilder().ToString();
                context.Response.Write(html);
            }
            else
            {
                if (dataaccess(username, password))
                {
                    context.Session["username"] = username;
                    context.Response.Redirect("Index.ashx");
                }
                else
                {

                    VelocityEngine vltEngine = new VelocityEngine();
                    vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                    vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
                    vltEngine.Init();

                    VelocityContext vltContext = new VelocityContext();
                    //vltContext.Put("p", person);//设置参数,在模板中可以通过$data来引用
                    vltContext.Put("username", username);
                    vltContext.Put("password", password);
                    vltContext.Put("msg", "用户名密码错误");

                    Template vltTemplate = vltEngine.GetTemplate("Front/login.html");//模版文件所在位置

                    System.IO.StringWriter vltWriter = new System.IO.StringWriter();
                    vltTemplate.Merge(vltContext, vltWriter);

                    string html = vltWriter.GetStringBuilder().ToString();
                    context.Response.Write(html);
                }
            }
        }
开发者ID:ujsxn,项目名称:UJSBookStore,代码行数:60,代码来源:Login.ashx.cs

示例4: RenderTemplate

        public string RenderTemplate(string masterPage, string templateName, IDictionary<string, object> data)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentException("The \"templateName\" parameter must be specified", "templateName");
            }

            var name = !string.IsNullOrEmpty(masterPage)
                 ? masterPage : templateName;

            var engine = new VelocityEngine();
            var props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _templatesPath);
            engine.Init(props);
            var template = engine.GetTemplate(name);
            template.Encoding = Encoding.UTF8.BodyName;
            var context = new VelocityContext();

            var templateData = data ?? new Dictionary<string, object>();
            foreach (var key in templateData.Keys)
            {
                context.Put(key, templateData[key]);
            }

            if (!string.IsNullOrEmpty(masterPage))
            {
                context.Put("childContent", templateName);
            }

            using (var writer = new StringWriter())
            {
                engine.MergeTemplate(name, context, writer);
                return writer.GetStringBuilder().ToString();
            }
        }
开发者ID:leon737,项目名称:MvcBlanket,代码行数:35,代码来源:NVelocityTemplateRepository.cs

示例5: readerHtml

        public static string readerHtml(string path, string name, object data)
        {
            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath(path));//模板文件所在的文件夹
            vltEngine.Init();

            VelocityContext vltContext = new VelocityContext();
            vltContext.Put("Data", data);//设置参数,在模板中可以通过$data来引用

            Template vltTemplate = vltEngine.GetTemplate(name);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            return vltWriter.GetStringBuilder().ToString();
        }
开发者ID:liuker0x007,项目名称:InfoSecPracticeSystem,代码行数:15,代码来源:writeHtml.cs

示例6: Test

        public void Test()
        {
            var vlte = new VelocityEngine();
            var props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory);
            vlte.Init(props);

            var vlc = new VelocityContext();
            vlc.Put("test", "test");

            var vtp = vlte.GetTemplate("NVelocityTest.vm");
            var str = new StringWriter();
            vtp.Merge(vlc, str);
            Console.WriteLine(str.GetStringBuilder().ToString());
        }
开发者ID:starlightliu,项目名称:Starlightliu,代码行数:16,代码来源:NVelocityTest.cs

示例7: ProcessRequest

        public void ProcessRequest(HttpContext context)
        {
            string searchid= context.Request.Form["searchid"];
            string searchtext = context.Request.Form["searchtext"];
            dataaccess(searchid, searchtext);
            if (searchid == "2")
            {
                //book
                context.Response.ContentType = "text/html";
                VelocityEngine vltEngine = new VelocityEngine();
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
                vltEngine.Init();
                VelocityContext vltContext = new VelocityContext();

                vltContext.Put("object", searchtext);
                string show="<img src='books/"+book.ser+"' width='210' height='150' />";
                vltContext.Put("result", show);

                Template vltTemplate = vltEngine.GetTemplate("Front/Search.html");//模版文件所在位置

                System.IO.StringWriter vltWriter = new System.IO.StringWriter();
                vltTemplate.Merge(vltContext, vltWriter);
                string html = vltWriter.GetStringBuilder().ToString();
                context.Response.Write(html);
            }
            else if (searchid == "3")
            {
                //news
                context.Response.ContentType = "text/html";
                VelocityEngine vltEngine = new VelocityEngine();
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
                vltEngine.Init();

                VelocityContext vltContext = new VelocityContext();

                vltContext.Put("object", searchtext);
                vltContext.Put("result", news.news);

                Template vltTemplate = vltEngine.GetTemplate("Front/Search.html");//模版文件所在位置
                System.IO.StringWriter vltWriter = new System.IO.StringWriter();
                vltTemplate.Merge(vltContext, vltWriter);
                string html = vltWriter.GetStringBuilder().ToString();
                context.Response.Write(html);
            }
        }
开发者ID:ujsxn,项目名称:UJSBookStore,代码行数:47,代码来源:Search.ashx.cs

示例8: FillResponseTemplate

 public virtual string FillResponseTemplate(IDictionary<string, object> viewParams)
 {
     var engine = new VelocityEngine();
     var props = new ExtendedProperties();
     props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "assembly");
     props.AddProperty("assembly.resource.loader.class",
                       "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity");
     props.AddProperty("assembly.resource.loader.assembly", GetType().Assembly.FullName);
     engine.Init(props);
     var vcontext = new VelocityContext();
     foreach (var k in viewParams) {
         vcontext.Put(k.Key, k.Value);
     }
     using (var writer = new StringWriter()) {
         engine.GetTemplate(ViewName).Merge(vcontext, writer);
         return writer.GetStringBuilder().ToString();
     }
 }
开发者ID:ruanzx,项目名称:mausch,代码行数:18,代码来源:Controller.cs

示例9: ProcessRequest

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            //创建一个模板引擎
            VelocityEngine vltEngine = new VelocityEngine();
            //文件型模板,还可以是 assembly ,则使用资源文件
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            //模板存放目录
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("/template"));//模板文件所在的文件夹
            vltEngine.Init();
            //定义一个模板上下文
            VelocityContext vltContext = new VelocityContext();
            //传入模板所需要的参数
            vltContext.Put("Title", "标题"); //设置参数,在模板中可以通过$Title来引用
            vltContext.Put("Body", "内容");  //设置参数,在模板中可以通过$Body来引用
            vltContext.Put("Date", DateTime.Now); //设置参数,在模板中可以通过$Date来引用
            //获取我们刚才所定义的模板,上面已设置模板目录
            Template vltTemplate = vltEngine.GetTemplate("basic.html");
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            //根据模板的上下文,将模板生成的内容写进刚才定义的字符串输出流中
            vltTemplate.Merge(vltContext, vltWriter);
            string html = vltWriter.GetStringBuilder().ToString();
            context.Response.Write(html);

            ////字符串模板源,这里就是你的邮件模板等等的字符串
            //const string templateStr = "$Title,$Body,$Date";

            ////创建一个模板引擎
            //VelocityEngine vltEngine = new VelocityEngine();
            ////文件型模板,还可以是 assembly ,则使用资源文件
            //vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            //vltEngine.Init();
            ////定义一个模板上下文
            //VelocityContext vltContext = new VelocityContext();
            ////传入模板所需要的参数
            //vltContext.Put("Title", "标题"); //设置参数,在模板中可以通过$Title来引用
            //vltContext.Put("Body", "内容");  //设置参数,在模板中可以通过$Body来引用
            //vltContext.Put("Date", DateTime.Now); //设置参数,在模板中可以通过$Date来引用
            ////定义一个字符串输出流
            //StringWriter vltWriter = new StringWriter();
            ////输出字符串流中的数据
            //vltEngine.Evaluate(vltContext, vltWriter, null, templateStr);
            //context.Response.Write(vltWriter.GetStringBuilder().ToString());
        }
开发者ID:proson,项目名称:project,代码行数:44,代码来源:basic.ashx.cs

示例10: Generate

        /// <summary>
        /// Generates a report filled with the content supplied by <paramref name="report"/>.
        /// </summary>
        /// <param name="report">Specifies the report model.</param>
        public void Generate(IReport report)
        {
            var engine = new VelocityEngine();
            var properties = new ExtendedProperties();
            properties.AddProperty("resource.loader", "assembly");
            properties.AddProperty("resource.manager.class", typeof(ResourceManagerImpl).AssemblyQualifiedName);
            properties.AddProperty("directive.manager", EncodeExternalAssemblyQualifiedType(typeof(DirectiveManagerProxy)));
            properties.AddProperty("assembly.resource.loader.class", typeof(AssemblyResourceLoader).AssemblyQualifiedName);
            properties.AddProperty("assembly.resource.loader.assembly", GetType().Assembly.GetName().Name);
            engine.Init(properties);

            var htmlTemplate = engine.GetTemplate("Xunit.Reporting.Internal.Generator.HtmlTemplate.vm");
            var generationContext = new VelocityContext();
            generationContext.Put("report", report);
            generationContext.Put("pluralizer", new Pluralizer());

            _outputWriter.Write(
                string.Concat(report.ReflectedAssembly, ".html"),
                writer => htmlTemplate.Merge(generationContext, writer));
        }
开发者ID:BjRo,项目名称:xunitbddextensions,代码行数:24,代码来源:HtmlReportGenerator.cs

示例11: GetCodeFileString

        public static string GetCodeFileString(HttpServerUtility server, CustomItemInformation info)
        {
            VelocityEngine velocity = new VelocityEngine();

            ExtendedProperties props = new ExtendedProperties();
            props.SetProperty("file.resource.loader.path", server.MapPath(".")); // The base path for Templates

            velocity.Init(props);

            //Template template = velocity.GetTemplate("template.tmp");
            NVelocity.Template template = velocity.GetTemplate("CustomItem.vm");

            VelocityContext context = new VelocityContext();

            context.Put("BaseTemplates", info.BaseTemplates);
            context.Put("CustomItemFields", info.Fields);
            context.Put("CustomItemInformation", info);

            StringWriter writer = new StringWriter();
            template.Merge(context, writer);
            return writer.GetStringBuilder().ToString();
        }
开发者ID:kmazzoni,项目名称:Custom-Item-Generator,代码行数:22,代码来源:CodeUtil.cs

示例12: ProcessRequest

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
            vltEngine.Init();

            VelocityContext vltContext = new VelocityContext();

            //vltContext.Put("msg", "");
            dataaccess();

            vltContext.Put("NewsPool", NewsPool);
            vltContext.Put("s", new GotNews());

            Template vltTemplate = vltEngine.GetTemplate("Front/News.html");//模版文件所在位置
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            string html = vltWriter.GetStringBuilder().ToString();
            context.Response.Write(html);
        }
开发者ID:ujsxn,项目名称:UJSBookStore,代码行数:22,代码来源:News.ashx.cs

示例13: GetFileText

 /// <summary>
 /// 通过模版文件路径读取文件内容
 /// </summary>
 /// <param name="path">模版文件路径</param>
 /// <param name="ht">模版文件的参数</param>
 /// <returns>StringWriter对象</returns>
 public static StringWriter GetFileText(string path, Hashtable ht)
 {
     if (String.IsNullOrEmpty(path))
     {
         throw new ArgumentNullException("模版文件路径为空!");
     }
     try
     {
         string tmpPath = path.Substring(0,path.LastIndexOf(@"\"));
         string filePath = path.Substring(path.LastIndexOf(@"\")+1);
         //创建NVelocity引擎的实例对象
         VelocityEngine velocity = new VelocityEngine();
         ExtendedProperties props = new ExtendedProperties();
         props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
         props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, tmpPath);
         props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
         props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
         props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
         velocity.Init(props);
         //从文件中读取模板
         Template temp = velocity.GetTemplate(filePath);
         IContext context = new VelocityContext();
         foreach (string key in ht.Keys)
         {
             context.Put(key, ht[key]);
         }
         //合并模板
         StringWriter writer = new StringWriter();
         temp.Merge(context, writer);
         return writer;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
开发者ID:Zorbam,项目名称:TaskManager,代码行数:42,代码来源:FileGen.cs

示例14: ProcessRequest

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            VelocityEngine velocityEngine = new VelocityEngine();
            velocityEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            velocityEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("/template"));
            velocityEngine.Init();

            //定义一个模板上下文
            VelocityContext velocityContext = new VelocityContext();
            //定义类对象
            Person p = new Person()
            {
                Name = "王三",
                Age = 20

            };
            velocityContext.Put("p", p);
            //定义键值对
            Dictionary<string,string> dic=new Dictionary<string, string>();
            dic["aa"] = "李四";
            dic["bb"] = "30";
            velocityContext.Put("dic", dic);

            //定义list集合
            List<string> list=new List<string>(){"张五","马六"};
            velocityContext.Put("list", list);

            //获取定义的模板
            Template template = velocityEngine.GetTemplate("baseobject.html");
            StringWriter stringWriter = new StringWriter();
            template.Merge(velocityContext, stringWriter);
            string html = stringWriter.GetStringBuilder().ToString();
            context.Response.Write(html);
        }
开发者ID:proson,项目名称:project,代码行数:36,代码来源:baseobject.ashx.cs

示例15: InitializeTask

		protected override void InitializeTask(XmlNode taskNode)
		{
			base.InitializeTask(taskNode);

			// Initializes NVelocity

			velocity = new VelocityEngine();

			ExtendedProperties props = new ExtendedProperties();
			velocity.Init(props);

			template = velocity.GetTemplate(templateFile);

			// TODO: validate all arguments are present
			
			counter = startOrder + 1;
		}
开发者ID:ralescano,项目名称:castle,代码行数:17,代码来源:NDocTransformTask.cs


注:本文中的NVelocity.App.VelocityEngine.GetTemplate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。