public static void Main(System.String[] args) { // first, we init the runtime engine. Defaults are fine. try { Velocity.Init(); } catch (System.Exception e) { System.Console.Out.WriteLine("Problem initializing Velocity : " + e); return; } // lets make a Context and put data into it VelocityContext context = new VelocityContext(); context.Put("name", "Velocity"); context.Put("project", "Jakarta"); // lets render a template StringWriter writer = new StringWriter(); try { Velocity.MergeTemplate("example2.vm", context, writer); } catch (System.Exception e) { System.Console.Out.WriteLine("Problem merging template : " + e); } System.Console.Out.WriteLine(" template : " + writer.GetStringBuilder().ToString()); // lets dynamically 'create' our template // and use the evaluate() method to render it System.String s = "We are using $project $name to render this."; writer = new StringWriter(); try { Velocity.Evaluate(context, writer, "mystring", s); } catch (ParseErrorException pee) { // thrown if something is wrong with the // syntax of our template string System.Console.Out.WriteLine("ParseErrorException : " + pee); } catch (MethodInvocationException mee) { // thrown if a method of a reference // called by the template // throws an exception. That won't happen here // as we aren't calling any methods in this // example, but we have to catch them anyway System.Console.Out.WriteLine("MethodInvocationException : " + mee); } catch (System.Exception e) { System.Console.Out.WriteLine("Exception : " + e); } System.Console.Out.WriteLine(" string : " + writer.GetStringBuilder().ToString()); }
public static bool Evaluate(IContext context, TextWriter writer, string logTag, Stream instream) { TextReader reader = null; string text = null; try { text = RuntimeSingleton.getString("input.encoding", "ISO-8859-1"); reader = new StreamReader(new StreamReader(instream, Encoding.GetEncoding(text)).BaseStream); } catch (IOException innerException) { string exceptionMessage = "Unsupported input encoding : " + text + " for template " + logTag; throw new ParseErrorException(exceptionMessage, innerException); } return(Velocity.Evaluate(context, writer, logTag, reader)); }
public static bool InvokeVelocimacro(string vmName, string logTag, string[] parameters, IContext context, TextWriter writer) { bool result; if (vmName == null || parameters == null || context == null || writer == null || logTag == null) { RuntimeSingleton.Error("Velocity.invokeVelocimacro() : invalid parameter"); result = false; } else if (!RuntimeSingleton.IsVelocimacro(vmName, logTag)) { RuntimeSingleton.Error("Velocity.invokeVelocimacro() : VM '" + vmName + "' not registered."); result = false; } else { StringBuilder stringBuilder = new StringBuilder("#"); stringBuilder.Append(vmName); stringBuilder.Append("("); for (int i = 0; i < parameters.Length; i++) { stringBuilder.Append(" $"); stringBuilder.Append(parameters[i]); } stringBuilder.Append(" )"); try { bool flag = Velocity.Evaluate(context, writer, logTag, stringBuilder.ToString()); result = flag; return(result); } catch (System.Exception arg) { RuntimeSingleton.Error("Velocity.invokeVelocimacro() : error " + arg); } result = false; } return(result); }
public static bool Evaluate(IContext context, TextWriter writer, string logTag, string instring) { return(Velocity.Evaluate(context, writer, logTag, new StringReader(instring))); }