public void Init(IDictionary<string, string> properties) { //初始化NVelocity引擎 var nv_properties = new Commons.Collections.ExtendedProperties(); var prefix = this.GetType().FullName + "."; foreach (String key in properties.Keys.Where(t => t.StartsWith(prefix))) nv_properties.SetProperty(key.Substring(prefix.Length), properties[key]); engine = new VelocityEngine(nv_properties); engine.Init(); }
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); }
static NVelocityHelper() { _velocity = new VelocityEngine(); var props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, ConfigurationManager.AppSettings["TemplateFolder"]); _velocity.Init(props); }
static NVelocity() { filePath = Utils.GetMapPath(BaseConfigs.GetForumPath); engine = new VelocityEngine(); props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, filePath); engine.Init(props); }
private static string ApplyTemplate(string template, VelocityContext context) { VelocityEngine velocity = new VelocityEngine(); ExtendedProperties props = new ExtendedProperties(); velocity.Init(props); using (var writer = new StringWriter(CultureInfo.CurrentCulture)) { velocity.Evaluate(context, writer, string.Empty, template); return writer.GetStringBuilder().ToString(); } }
/// <summary> /// Initializes a new instance of the <see cref="NVelocityViewEngine"/> class. /// </summary> /// <param name="assemblies">The assemblies.</param> /// <param name="extensionMethodTypes">The extension method types.</param> public NVelocityViewEngine(IEnumerable<Assembly> assemblies, params Type[] extensionMethodTypes) { var properties = new ExtendedProperties(); properties.AddProperty("resource.loader", "parials"); properties.AddProperty("parials.resource.loader.class", typeof(PartialFileResourceLoader).AssemblyQualifiedName.Replace(",", ";")); properties.AddProperty("parials.resource.loader.assembly", assemblies.Select(a => a.FullName).ToList()); _engine = new VelocityEngine(); _engine.Init(properties); _extensionMethods = extensionMethodTypes.SelectMany(type => type.GetMethods(BindingFlags.Public | BindingFlags.Static)).Select(method => new DynamicDispatchMethod(method)).ToArray(); }
static HtmlPage() { Engine = new VelocityEngine(); //Engine = NVelocityTemplateEngine.NVelocityEngineFactory.CreateNVelocityAssemblyEngine(Assembly.GetExecutingAssembly().GetName().Name, false); //Engine = NVelocityTemplateEngine.NVelocityEngineFactory.CreateNVelocityMemoryEngine(true); var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WebServerTestApp.Views.default.aspx.htm"); var sr = new StreamReader(stream); template = sr.ReadToEnd(); sr.Dispose(); ExtendedProperties props = new ExtendedProperties(); Engine.Init(props); }
public void Setup() { items = new ArrayList(); items.Add("a"); items.Add("b"); items.Add("c"); items.Add("d"); ve = new VelocityEngine(); ve.Init(); }
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(); } }
public void SetUp() { ve = new VelocityEngine(); ve.Init(); ctx = new VelocityContext(); }
/// <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; }
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); } } }
public void Setup() { context = new VelocityContext(); ve = new VelocityEngine(); ve.Init(); }
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(); }
public void Initialize() { ActiveRecordStarter.Initialize( new XmlConfigurationSource("activeRecord.xml"), typeof(Acl) , typeof(Category) , typeof(Chat) , typeof(ChatMessage) , typeof(ConfigCombo) , typeof(ConfigModel) , typeof(Container) , typeof(Content) , typeof(DataModel) , typeof(Field) , typeof(FieldTemplate) , typeof(CastlePortal.File) , typeof(Forum) , typeof(ForumFolder) , typeof(ForumMessage) , typeof(Group) , typeof(Menu) , typeof(Role) , typeof(CastlePortal.Template) , typeof(CastlePortal.Type) , typeof(Language), typeof(MenuTranslation), typeof(TypeTranslation), typeof(User) ); velocity = new VelocityEngine(); ExtendedProperties props = new ExtendedProperties(); velocity.Init(props); }
/// <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); } }
private VelocityEngine CreateVelocityEngine() { var velocity = new VelocityEngine(); var props = new ExtendedProperties(); props.SetProperty("file.resource.loader.path", _templateDirectory); velocity.Init(props); return velocity; }
public NVelocityTemplateRepository(string templateDirectory) { engine = new VelocityEngine(); ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDirectory); engine.Init(props); }
/// <inheritdoc /> public VelocityEngine CreateVelocityEngine() { var properties = new ExtendedProperties(); SetupVelocityEngine(properties); var engine = new VelocityEngine(properties); engine.Init(); return engine; }
public Helper() { vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory); vltEngine.Init(); }
public virtual bool Configure() { _engine = new VelocityEngine(); var props = new ExtendedProperties(); props.AddProperty("file.resource.loader.path", TemplatePath); _engine.Init(props); return true; }
public TemplateHelper(string templatePath) { velocity = new VelocityEngine(); ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); velocity.Init(props); context = new VelocityContext(); }
private VelocityEngine GetInitialisedEngine() { VelocityEngine engine = new VelocityEngine(); ExtendedProperties properties = new ExtendedProperties(); properties.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); properties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, agencyService.CurrentAgency.TemplateDirectory); engine.Init(properties); return engine; }
static Program() { VelocityEngine velocity = new VelocityEngine(); velocity.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); velocity.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, @"C:\test\Com.Hertkorn.DevelopmentTree\Com.Hertkorn.DevelopmentTree\template"); velocity.Init(); Engine = velocity; }
/// <summary> /// Creates an instance of the EmailBuilder using the specified properties. /// </summary> /// <param name="nvelocityProperties"></param> public EmailBuilder(IDictionary<string, object> nvelocityProperties) { var properties = new ExtendedProperties(); foreach (var pair in nvelocityProperties) { properties.AddProperty(pair.Key, pair.Value); } velocityEngine = new VelocityEngine(); velocityEngine.Init(properties); }
public void DoesNotGiveNullReferenceExceptionWhenAmbiguousMatchBecauseOfNullArg() { VelocityEngine engine = new VelocityEngine(); engine.Init(); VelocityContext ctx = new VelocityContext(); ctx.Put("test", new TestClass()); ctx.Put("nullObj", null); StringWriter writer = new StringWriter(); Assert.IsTrue(engine.Evaluate(ctx, writer, "testEval", "$test.DoSomething($nullObj)")); Assert.AreEqual("$test.DoSomething($nullObj)", writer.GetStringBuilder().ToString()); }
public string Evaluate(string template, IDictionary<string, object> context) { var velocityContext = new VelocityContext(); foreach (var keyValuePair in context) { velocityContext.Put(keyValuePair.Key, keyValuePair.Value); } var velocity = new VelocityEngine(); velocity.Init(); var writer = new StringWriter(); velocity.Evaluate(velocityContext, writer, null, template); return writer.GetStringBuilder().ToString(); }
private static MailData RenderTemplateInternal(MailTemplate template, IEnumerable<KeyValuePair<string, object>> data) { var engine = new VelocityEngine(); engine.Init(); var context = GetContext(data); var result = new MailData { Subject = EvaluateTemplate(engine, context, template.Subject), Body = EvaluateTemplate(engine, context, template.Content) }; return result; }
public NVelocityHelper(string key, object value) { //1.创建Velocity 引擎(VelocityEngine)并设置属性 velocityEngine = new VelocityEngine(); velocityEngine.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // Velocity加载类型 velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, // Velocity加载文件路径 HttpContext.Current.Server.MapPath("~/Templates/")); velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); // 输入编码格式设置 velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); // 输出编码格式设置 velocityEngine.Init(); //2.Velocity 上下文对象设置 vc = new VelocityContext(); vc.Put(key, value); }
public void Setup() { ve = new VelocityEngine(); ve.Init(); // creates the context... c = new VelocityContext(); // attach a new event cartridge c.AttachEventCartridge(new EventCartridge()); // add our custom handler to the ReferenceInsertion event c.EventCartridge.ReferenceInsertion += new ReferenceInsertionEventHandler(EventCartridge_ReferenceInsertion); }
/// <summary> /// Initialize the engine with required properties. /// </summary> /// <returns>Engine instance</returns> private static VelocityEngine GetEngine() { NVelocity.App.VelocityEngine engine = new NVelocity.App.VelocityEngine(); //set log class type engine.SetProperty(NVelocity.Runtime.RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, typeof(TemplateEngineLog)); //switch to local context, this way each macro/recursive execution uses its own variables. engine.SetProperty(NVelocity.Runtime.RuntimeConstants.VM_CONTEXT_LOCALSCOPE, "true"); //allows #set to accept null values in the right hand side. engine.SetProperty(NVelocity.Runtime.RuntimeConstants.SET_NULL_ALLOWED, "true"); //set template resource loader to strings engine.SetProperty("resource.loader", "string"); engine.SetProperty("string.resource.loader.class", "NVelocity.Runtime.Resource.Loader.StringResourceLoader"); //engine.SetProperty("string.resource.loader.repository.class", "NVelocity.Runtime.Resource.Util.StringResourceRepositoryImpl"); //initialize engine. engine.Init(); return(engine); }