Ejemplo n.º 1
0
    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 Example1(System.String templateFile)
    {
        try {
            /*
             * setup
             */
            Velocity.Init("nvelocity.properties");

            /*
             *  Make a context object and populate with the data.  This
             *  is where the Velocity engine gets the data to resolve the
             *  references (ex. $list) in the template
             */
            VelocityContext context = new VelocityContext();
            context.Put("list", Names);

            /*
             *  get the Template object.  This is the parsed version of your
             *  template input file.  Note that getTemplate() can throw
             *   ResourceNotFoundException : if it doesn't find the template
             *   ParseErrorException : if there is something wrong with the VTL
             *   Exception : if something else goes wrong (this is generally
             *        indicative of as serious problem...)
             */
            Template template = null;

            try {
                template = Velocity.GetTemplate(templateFile)
                ;
            } catch (ResourceNotFoundException) {
                System.Console.Out.WriteLine("Example1 : error : cannot find template " + templateFile);
            } catch (ParseErrorException pee) {
                System.Console.Out.WriteLine("Example1 : Syntax error in template " + templateFile + ":" + pee);
            }

            /*
             *  Now have the template engine process your template using the
             *  data placed into the context.  Think of it as a  'merge'
             *  of the template and the data to produce the output stream.
             */
            if (template != null)
            {
                template.Merge(context, System.Console.Out);
            }
        } catch (System.Exception e) {
            System.Console.Out.WriteLine(e);
        }
    }