Package org.apache.velocity

Examples of org.apache.velocity.Template


    {
        // then get a context
        Context context = getContext(request);

        // get the template
        Template template = getTemplate(request);

        // merge the template and context into the writer
        merge(template, context, out);

        return context;
View Full Code Here


        {
            VelocityView view = getVelocityView();
            ViewToolContext context = getViewToolContext();

            // get the actual Template
            Template template = view.getTemplate(getTemplate());

            if (getBodyContent() != null)
            {
                context.put(getBodyContentKey(), getRenderedBody());
            }

            // render the template into the writer
            template.merge(context, out);
        }
        else
        {
            // render the body into the writer
            renderBody(out);
View Full Code Here

   
    public Email createEmail(String templatePath, String subject, String emailAddress) {
        init();
        StringWriter writer = new StringWriter();
        try {
            Template t = ve.getTemplate(templatePath);
            t.merge(context, writer);
        } catch (Exception e) {
            throw new ApplicationException(e);
        }

        Email email = newTransientInstance(Email.class);
View Full Code Here

             *   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 = Velocity.getTemplate(view);
           
            /*
             *  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.
             */
           response.setContentType(contentType);
           writer = response.getWriter()// BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
           
           template.merge(context, writer);
           writer.flush()// flush and cleanup
        }
        catch(ResourceNotFoundException e) {
          throw new RenderException("Example : error : cannot find template " + view, e);
        }
View Full Code Here

             *   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 = Velocity.getTemplate(view);
           
            /*
             *  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.
             */
           response.setContentType(contentType);
           writer = response.getWriter()// BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
           
           template.merge(context, writer);
           writer.flush()// flush and cleanup
        }
        catch(ResourceNotFoundException e) {
          throw new RenderException("Example : error : cannot find template " + view, e);
        }
View Full Code Here

         *   Exception : if something else goes wrong (this is generally
         *        indicative of a serious problem...)
         */
        try
        {
            Template template = Velocity.getTemplate( templateLocation );
            /*
             *  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.
             */
            FileWriter writer = new FileWriter( outputFile );
            if ( template != null )
            {
                template.merge( context, writer );
            }
            writer.close();
        }
        catch ( Exception e )
        {
View Full Code Here

        context.put("parameters", parameters);
        context.put("runtimeClass", runtimeClass);
        context.put("requestClass", requestClass);
        try {
            file.createNewFile();
            Template template = velocityEngine.getTemplate(getTemplatePath(dir,
                    packageName, suite, clazz, parameters, runtimeClass, requestClass));
            Writer writer = new FileWriter(file);
            try {
                template.merge(context, writer);
            } finally {
                writer.close();
            }
        } catch (ResourceNotFoundException e) {
            throw new AutotagRuntimeException("Cannot find template resource",
View Full Code Here

        /*
         *  now use the Runtime resource loader to get the template
         */

        Template t = null;

        try
        {
            if (!blockinput)
                t = rsvc.getTemplate( arg, getInputEncoding(context) );
        }
        catch ( ResourceNotFoundException rnfe )
        {
            /*
             * the arg wasn't found.  Note it and throw
             */
            rsvc.getLog().error("#parse(): cannot find template '" + arg +
                                "', called from template " +
                                context.getCurrentTemplateName() + " at (" +
                                getLine() + ", " + getColumn() + ")" );
            throw rnfe;
        }
        catch ( ParseErrorException pee )
        {
            /*
             * the arg was found, but didn't parse - syntax error
             *  note it and throw
             */

            rsvc.getLog().error("#parse(): syntax error in #parse()-ed template '"
                                + arg + "', called from template " +
                                context.getCurrentTemplateName() + " at (" +
                                getLine() + ", " + getColumn() + ")" );

            throw pee;
        }
        /**
         * pass through application level runtime exceptions
         */
        catch( RuntimeException e )
        {
            throw e;
        }
        catch ( Exception e)
        {
            String msg = "#parse() : arg = " + arg + '.';
            rsvc.getLog().error(msg, e);
            throw new VelocityException(msg, e);
        }

        /**
         * Add the template name to the macro libraries list
         */
        List macroLibraries = context.getMacroLibraries();

        /**
         * if macroLibraries are not set create a new one
         */
        if (macroLibraries == null)
        {
            macroLibraries = new ArrayList();
        }

        context.setMacroLibraries(macroLibraries);

        macroLibraries.add(arg);

        /*
         *  and render it
         */
        try
        {
            if (!blockinput) {
                context.pushCurrentTemplateName(arg);
                ((SimpleNode) t.getData()).render( context, writer );
            }
        }

        /*
         *  if it's a MIE, it came from the render.... throw it...
View Full Code Here

    @Test
    public void testGenerate() throws Exception {
        directory.delete();
        directory.mkdir();
        TemplateSuite suite = createMock(TemplateSuite.class);
        Template template = createMock(Template.class);
        @SuppressWarnings("unchecked")
        Map<String, String> parameters = createMock(Map.class);
        String packageName = "org.apache.tiles.autotag.test";

        expect(generator.getDirectoryName(directory, packageName, suite, parameters)).andReturn("mydir");
        File mydir = new File(directory, "mydir");
        expect(generator.getFilename(mydir , packageName, suite, parameters)).andReturn("myfile.txt");
        String sampleVmPath = "/sample.vm";
        expect(generator.getTemplatePath(mydir, packageName, suite, parameters)).andReturn(sampleVmPath);
        expect(velocityEngine.getTemplate("/sample.vm")).andReturn(template);
        template.merge(isA(VelocityContext.class), isA(FileWriter.class));

        replay(velocityEngine, generator, suite, template, parameters);
        generator.generate(directory, packageName, suite, parameters);
        verify(velocityEngine, generator, suite, template, parameters);
    }
View Full Code Here

    @Test(expected = AutotagRuntimeException.class)
    public void testGenerateException1() throws Exception {
        directory.delete();
        directory.mkdir();
        TemplateSuite suite = createMock(TemplateSuite.class);
        Template template = createMock(Template.class);
        @SuppressWarnings("unchecked")
        Map<String, String> parameters = createMock(Map.class);
        String packageName = "org.apache.tiles.autotag.test";

        expect(generator.getDirectoryName(directory, packageName, suite, parameters)).andReturn("mydir");
View Full Code Here

TOP

Related Classes of org.apache.velocity.Template

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.