當前位置: 首頁>>代碼示例>>C#>>正文


C# VelocityContext.Put方法代碼示例

本文整理匯總了C#中NVelocity.VelocityContext.Put方法的典型用法代碼示例。如果您正苦於以下問題:C# VelocityContext.Put方法的具體用法?C# VelocityContext.Put怎麽用?C# VelocityContext.Put使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在NVelocity.VelocityContext的用法示例。


在下文中一共展示了VelocityContext.Put方法的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: RenderView

        public static string RenderView(this HtmlHelper html, string viewName, string sectionName, IDictionary parameters)
        {
            if (!string.IsNullOrEmpty(sectionName))
            {
                viewName += ":" + sectionName;
            }

            var velocityView = html.ViewContext.View as NVelocityView;
            if (velocityView == null) throw new InvalidOperationException("The RenderView extension can only be used from views that were created using NVelocity.");

            var newContext = velocityView.Context;
            if (parameters != null && parameters.Keys.Count > 0)
            {
                // Clone the existing context and then add the custom parameters
                newContext = new VelocityContext();
                foreach (var key in velocityView.Context.Keys)
                {
                    newContext.Put((string)key, velocityView.Context.Get((string)key));
                }
                foreach (var key in parameters.Keys)
                {
                    newContext.Put((string)key, parameters[key]);
                }
            }

            // Resolve the template and render it. The partial resource loader takes care of validating
            // the view name and extracting the partials.
            var template = velocityView.Engine.GetTemplate(viewName);
            using (var writer = new StringWriter())
            {
                template.Merge(newContext, writer);
                return writer.ToString();
            }
        }
開發者ID:PaulStovell,項目名稱:bindable,代碼行數:34,代碼來源:NVelocityExtensions.cs

示例3: ExecuteMethodUntilSignal1

        /// <summary>
        /// This test starts a VelocityEngine for each execution
        /// </summary>
        public void ExecuteMethodUntilSignal1()
        {
            startEvent.WaitOne(int.MaxValue, false);

            while(!stopEvent.WaitOne(0, false))
            {
                VelocityEngine velocityEngine = new VelocityEngine();
                velocityEngine.Init();

                StringWriter sw = new StringWriter();

                VelocityContext c = new VelocityContext();
                c.Put("x", new Something());
                c.Put("items", items);

                bool ok = velocityEngine.Evaluate(c, sw,
                                                  "ContextTest.CaseInsensitive",
                                                  @"
                    #foreach( $item in $items )
                        $item,
                    #end
                ");

                Assert.IsTrue(ok, "Evaluation returned failure");
                Assert.AreEqual("a,b,c,d,", Normalize(sw));
            }
        }
開發者ID:hatjhie,項目名稱:NVelocity,代碼行數:30,代碼來源:MultiThreadTestCase.cs

示例4: RenderTemplate

        public void RenderTemplate(string templateName, object model)
        {
            string templateFullPath = applicationInfo.AbsolutizePath("Web/Pages/Templates/" + templateName + ".vm.html");

            ITemplateSource template = new CacheableFileTemplate(
                templateFullPath,
                fileCache);
            string templateText = template.GetTemplate();

            VelocityEngine velocity = new VelocityEngine();
            velocity.Init();

            VelocityContext velocityContext = new VelocityContext();
            velocityContext.Put("m", model);
            velocityContext.Put("h", this);

            using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
            {
                if (false == velocity.Evaluate(velocityContext, stringWriter, null, templateText))
                    throw new InvalidOperationException("Template expansion failed");

                writer.InnerWriter.Write(stringWriter.ToString());
            }

            writer.WriteLine();
        }
開發者ID:Ryks,項目名稱:detergent,代碼行數:26,代碼來源:NVelocityHtmlRenderHelper.cs

示例5: 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

示例6: CreateVelocityContext

        private IContext CreateVelocityContext(object model)
        {
            var velocityContext = new VelocityContext();

            if (model != null)
                velocityContext.Put("model", model);

            velocityContext.Put("vel", this);
            velocityContext.Put("html", new HtmlHelper());

            return velocityContext;
        }
開發者ID:Leafney,項目名稱:SimpleHttpServer,代碼行數:12,代碼來源:NVelocityHelper.cs

示例7: CreateTemplateContext

        private VelocityContext CreateTemplateContext(ProcessURI uri, IProcessResponse response, Domain domain)
        {
            VelocityContext context = new VelocityContext();
            context.Put("uri", uri);
            context.Put("renderer", this);

            if (response != null) context.Put("response", response);
            if (domain != null) context.Put("domain", domain);

            foreach (RendererHelper helper in Helpers)
                context.Put(helper.Name, helper);

            return context;
        }
開發者ID:timbooker,項目名稱:dotObjects,代碼行數:14,代碼來源:NVelocityRenderer.cs

示例8: FormatUserEntry

		public string FormatUserEntry(User user, ISettings settings, string template)
		{
			var context = new VelocityContext();
			context.Put("user", user);
			context.Put("settings", settings);

			using (StringWriter writer = new StringWriter())
			{
				template = PrepareTemplate(template);
				_engine.Evaluate(context, writer, null, template);

				return writer.ToString();
			}
		}
開發者ID:agross,項目名稱:netopenspace,代碼行數:14,代碼來源:NVelocityEntryFormatter.cs

示例9: GetTemplatedDocument

        public String GetTemplatedDocument(String templateName, IDictionary<String, String> data, IDictionary<String, object[]> list = null)
        {
            Template template = _engine.GetTemplate(templateName);

            VelocityContext context = new VelocityContext();
            data.Keys.ToList().ForEach(k => context.Put(k, data[k]));

            if (list != null)
                context.Put(list.Keys.FirstOrDefault(), list.Values.FirstOrDefault());

            StringWriter writer = new StringWriter();
            template.Merge(context, writer);

            return writer.ToString();
        }
開發者ID:bea-project,項目名稱:bea-web,代碼行數:15,代碼來源:TemplatingService.cs

示例10: GetRealMessage

        public string GetRealMessage(BaseRequest request, Response response)
        {
            var baseMessage = GetBaseMessage();
            if (baseMessage == null)
                return null;

            var context = new VelocityContext();
            context.Put("request", request);
            context.Put("response", response);

            var result = new StringWriter();

            Velocity.Evaluate(context, result, "", baseMessage);
            return result.ToString();
        }
開發者ID:julienblin,項目名稱:Colombo.Clerk,代碼行數:15,代碼來源:ClerkMessageAttribute.cs

示例11: BuildEmail

		/// <exception cref="ArgumentException"></exception>
		string BuildEmail(string templateName, IDictionary<string, object> viewdata)
		{
			if (viewdata == null)
			{
				throw new ArgumentNullException("viewData");
			}

			if (string.IsNullOrEmpty(templateName))
			{
				throw new ArgumentException("TemplateName");
			}

			var template = ResolveTemplate(templateName);

			var context = new VelocityContext();

			foreach (var key in viewdata.Keys)
			{
				context.Put(key, viewdata[key]);
			}

			using (var writer = new StringWriter())
			{
				template.Merge(context, writer);
				return writer.ToString();
			}
		}
開發者ID:sthapa123,項目名稱:sutekishop,代碼行數:28,代碼來源:EmailBuilder.cs

示例12: 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

示例13: FormatText

 public static string FormatText(string templateText, IDictionary<string,object> values)
 {
     var nvelocityContext = new VelocityContext();
     foreach (var tagValue in values)
         nvelocityContext.Put(tagValue.Key, tagValue.Value);
     return FormatText(templateText, nvelocityContext);
 }
開發者ID:haoasqui,項目名稱:ONLYOFFICE-Server,代碼行數:7,代碼來源:VelocityFormatter.cs

示例14: Format

        /// <summary>The format.</summary>
        /// <param name="text">The text.</param>
        /// <param name="items">The items.</param>
        /// <returns>The format.</returns>
        /// <exception cref="TemplateException"></exception>
        public string Format(string text, Dictionary<string, object> items)
        {
            try
            {
                VelocityContext velocityContext = new VelocityContext();

                if (items != null)
                {
                    foreach (var pair in items)
                    {
                        velocityContext.Put(pair.Key, pair.Value);
                    }
                }

                StringWriter sw = new StringWriter();
                VelocityEngine velocityEngine = new VelocityEngine();
                velocityEngine.Init();

                bool ok = velocityEngine.Evaluate(velocityContext, sw, "ContextTest.CaseInsensitive", text);

                if (!ok)
                {
                    throw new TemplateException("Template run error (try adding an extra newline at the end of the file)");
                }

                return sw.ToString();
            }
            catch (ParseErrorException parseErrorException)
            {
                throw new TemplateException(parseErrorException.Message, parseErrorException);
            }
        }
開發者ID:ClusterReply,項目名稱:minisqlquery,代碼行數:37,代碼來源:NVelocityWrapper.cs

示例15: 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


注:本文中的NVelocity.VelocityContext.Put方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。