it is a little bit ugly to do that in Velocity, here is the way I do it
....

        //TODO: these stuff need to be injected
        VelocityEngine ve = new VelocityEngine();
        try {
            Properties p = new Properties();
            p.setProperty("resource.loader", "string");
            p
                    .setProperty("string.resource.loader.class",

"org.apache.velocity.runtime.resource.loader.StringResourceLoader");
            ve.init(p);
            StringResourceRepository vsRepository = StringResourceLoader
                    .getRepository();
            String myTemplateName = "maglink";
            String myTemplateBody = "Hi $name ! How do you do?" ;
            vsRepository.putStringResource(myTemplateName, myTemplateBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VelocityContext context = new VelocityContext();
        context.put(  "name" ,  "Youssef");
        Template template = null;
        try {
            template = ve.getTemplate("maglink");
            StringWriter sw = new StringWriter();
            template.merge(context, sw);
            assert  sw.toString().equals("Hi Youssef ! How do you do?")  ;

        } catch (ResourceNotFoundException rnfe) {
            // couldn't find the template
        } catch (ParseErrorException pee) {
            // syntax error: problem parsing the template
        } catch (MethodInvocationException mie) {
            // something invoked in the template
            // threw an exception
        } catch (Exception e) {
        }
        // TODO Auto-generated method stub




Regards, Youssef


On Fri, Jan 16, 2009 at 3:38 AM, Shailesh Patel <shailes...@gmail.com>wrote:

> Shan -
>
> You can use the evaluate method on a VelocityEngine instance -- pass in
> your own StringWriter:.
>
> See
> http://velocity.apache.org/engine/releases/velocity-1.4/api/org/apache/velocity/app/VelocityEngine.html#evaluate(org.apache.velocity.context.Context,%20java.io.Writer,%20java.lang.String,%20java.lang.String)<http://velocity.apache.org/engine/releases/velocity-1.4/api/org/apache/velocity/app/VelocityEngine.html#evaluate%28org.apache.velocity.context.Context,%20java.io.Writer,%20java.lang.String,%20java.lang.String%29>
>
> -Shailesh
>
>
>
> On Thu, Jan 15, 2009 at 4:32 PM, ashan <arthan...@gmail.com> wrote:
>
>>
>> I will try to ask this in Velocity forum. It would be very convenient if
>> we
>> have a overloaded VelocityUtil.mergeTemplatexx that takes InputStream
>> instead of template file name ;) .
>>
>> Thanks!
>>
>>
>>
>>
>> ashan wrote:
>> >
>> > I'm using appfuse 2.x. I need to let user to customize email content
>> > before sending. So I have a screen with textarea that shows velocity
>> > template so that user can add their additional contents / customize
>> > current content before sending.
>> >
>> > Is there a  way to pass this modified content to template engine without
>> > saving this into a file?
>> >
>> > Any other better  solution?
>> >
>> > Thanks!
>> > Shan
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Working-with-velocity-templates-tp21476180s2369p21487366.html
>> Sent from the AppFuse - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscr...@appfuse.dev.java.net
>> For additional commands, e-mail: users-h...@appfuse.dev.java.net
>>
>>
>

Reply via email to