You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Paulo Gaspar <pa...@krankikom.de> on 2001/11/29 03:35:51 UTC

Making templates from Strings

Hi,


In my code I use a small utility class to do something like
  org.apache.velocity.app.Velocity.evaluate()
but without parsing the template String every time I want to
render the template.

This class - appended to this message - is a simplification
from my code, was not tested and was made for a older
version of Velocity.

So, the questions are:
 - Is someone interested on this little thing?
 - Am I using the right classes, i.e.: should I really use
     org.apache.velocity.runtime.RuntimeInstance
   or should I use something else?

If you are interested, I will fix it according to the experts
remarks, test it and post it.


Have fun,
Paulo Gaspar


http://www.krankikom.de
http://www.ruhronline.de


import java.io.Writer;
import java.io.StringReader;
import java.io.Reader;
import java.io.StringWriter;

import org.apache.velocity.context.Context;
import org.apache.velocity.context.InternalContextAdapterImpl;
import org.apache.velocity.runtime.RuntimeInstance;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.apache.velocity.runtime.parser.ParseException;


class StaticTemplate
{   private RuntimeInstance  m_runtimeInstance;
    private SimpleNode       m_nodeTree = null;
    private String           m_logTag;

    protected StaticTemplate(RuntimeInstance i_runtimeInstance, String
i_logTag, Reader i_reader)
    throws ParseException
    {   super();

        m_logTag          = i_logTag;
        m_runtimeInstance = i_runtimeInstance;
        m_nodeTree        = m_runtimeInstance.parse(i_reader, m_logTag);
    }

    protected StaticTemplate( RuntimeInstance i_runtimeInstance,
                              String i_logTag, String i_templateText
                            )
    throws ParseException
    {   this(i_runtimeInstance, i_logTag, new StringReader(i_templateText));
    }

    private void render(Writer o_writer, Context i_ctx)
    throws Exception
    {   InternalContextAdapterImpl ica = new
InternalContextAdapterImpl(i_ctx);
        ica.pushCurrentTemplateName(m_logTag);
        m_nodeTree.init(ica, m_runtimeInstance);

        try
        {   m_nodeTree.render(ica, o_writer);
        }
        finally
        {   ica.popCurrentTemplateName();
        }
    }

    public String render(Context i_ctx)
    throws Exception
    {   Writer wrt = new StringWriter();
        render(wrt, i_ctx);
        return wrt.toString();
    }
}


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Making templates from Strings

Posted by Paulo Gaspar <pa...@krankikom.de>.
Hi Geir,


It looks like the same thing. Is this little class of any help?
(You probably have a dozen of little classes like this.)


Have fun,
Paulo Gaspar

> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: Thursday, November 29, 2001 4:11 AM
>
>
> On 11/28/01 9:35 PM, "Paulo Gaspar" <pa...@krankikom.de> wrote:
>
> > Hi,
> >
> >
> > In my code I use a small utility class to do something like
> > org.apache.velocity.app.Velocity.evaluate()
> > but without parsing the template String every time I want to
> > render the template.
>
> I've been wanting to add a helper method to Velocity and
> VelocityEngine that
> is like evaluate but returns a Template.
>
> Then you, the application programmer, can reuse them, and manage their
> storage any way you want - for example storing them in a map or something.
>
> I want to do that anyway - would that serve the same purpose as this? Or
> could you take advantage of it?
>
> geir
>
> > This class - appended to this message - is a simplification
> > from my code, was not tested and was made for a older
> > version of Velocity.
> >
> > So, the questions are:
> > - Is someone interested on this little thing?
> > - Am I using the right classes, i.e.: should I really use
> >    org.apache.velocity.runtime.RuntimeInstance
> >  or should I use something else?
> >
> > If you are interested, I will fix it according to the experts
> > remarks, test it and post it.
> >
> >
> > Have fun,
> > Paulo Gaspar
> >
> >
> > http://www.krankikom.de
> > http://www.ruhronline.de
> >
> >
> > import java.io.Writer;
> > import java.io.StringReader;
> > import java.io.Reader;
> > import java.io.StringWriter;
> >
> > import org.apache.velocity.context.Context;
> > import org.apache.velocity.context.InternalContextAdapterImpl;
> > import org.apache.velocity.runtime.RuntimeInstance;
> > import org.apache.velocity.runtime.parser.node.SimpleNode;
> > import org.apache.velocity.runtime.parser.ParseException;
> >
> >
> > class StaticTemplate
> > {   private RuntimeInstance  m_runtimeInstance;
> >   private SimpleNode       m_nodeTree = null;
> >   private String           m_logTag;
> >
> >   protected StaticTemplate(RuntimeInstance i_runtimeInstance, String
> > i_logTag, Reader i_reader)
> >   throws ParseException
> >   {   super();
> >
> >       m_logTag          = i_logTag;
> >       m_runtimeInstance = i_runtimeInstance;
> >       m_nodeTree        = m_runtimeInstance.parse(i_reader, m_logTag);
> >   }
> >
> >   protected StaticTemplate( RuntimeInstance i_runtimeInstance,
> >                             String i_logTag, String i_templateText
> >                           )
> >   throws ParseException
> >   {   this(i_runtimeInstance, i_logTag, new
> StringReader(i_templateText));
> >   }
> >
> >   private void render(Writer o_writer, Context i_ctx)
> >   throws Exception
> >   {   InternalContextAdapterImpl ica = new
> > InternalContextAdapterImpl(i_ctx);
> >       ica.pushCurrentTemplateName(m_logTag);
> >       m_nodeTree.init(ica, m_runtimeInstance);
> >
> >       try
> >       {   m_nodeTree.render(ica, o_writer);
> >       }
> >       finally
> >       {   ica.popCurrentTemplateName();
> >       }
> >   }
> >
> >   public String render(Context i_ctx)
> >   throws Exception
> >   {   Writer wrt = new StringWriter();
> >       render(wrt, i_ctx);
> >       return wrt.toString();
> >   }
> > }
> >
> >
> > --
> > To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> >
>
> --
> Geir Magnusson Jr.                                     geirm@optonline.net
> System and Software Consulting
> "He who throws mud only loses ground." - Fat Albert
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Making templates from Strings

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/28/01 9:35 PM, "Paulo Gaspar" <pa...@krankikom.de> wrote:

> Hi,
> 
> 
> In my code I use a small utility class to do something like
> org.apache.velocity.app.Velocity.evaluate()
> but without parsing the template String every time I want to
> render the template.

I've been wanting to add a helper method to Velocity and VelocityEngine that
is like evaluate but returns a Template.

Then you, the application programmer, can reuse them, and manage their
storage any way you want - for example storing them in a map or something.

I want to do that anyway - would that serve the same purpose as this? Or
could you take advantage of it?

geir

> This class - appended to this message - is a simplification
> from my code, was not tested and was made for a older
> version of Velocity.
> 
> So, the questions are:
> - Is someone interested on this little thing?
> - Am I using the right classes, i.e.: should I really use
>    org.apache.velocity.runtime.RuntimeInstance
>  or should I use something else?
> 
> If you are interested, I will fix it according to the experts
> remarks, test it and post it.
> 
> 
> Have fun,
> Paulo Gaspar
> 
> 
> http://www.krankikom.de
> http://www.ruhronline.de
> 
> 
> import java.io.Writer;
> import java.io.StringReader;
> import java.io.Reader;
> import java.io.StringWriter;
> 
> import org.apache.velocity.context.Context;
> import org.apache.velocity.context.InternalContextAdapterImpl;
> import org.apache.velocity.runtime.RuntimeInstance;
> import org.apache.velocity.runtime.parser.node.SimpleNode;
> import org.apache.velocity.runtime.parser.ParseException;
> 
> 
> class StaticTemplate
> {   private RuntimeInstance  m_runtimeInstance;
>   private SimpleNode       m_nodeTree = null;
>   private String           m_logTag;
> 
>   protected StaticTemplate(RuntimeInstance i_runtimeInstance, String
> i_logTag, Reader i_reader)
>   throws ParseException
>   {   super();
> 
>       m_logTag          = i_logTag;
>       m_runtimeInstance = i_runtimeInstance;
>       m_nodeTree        = m_runtimeInstance.parse(i_reader, m_logTag);
>   }
> 
>   protected StaticTemplate( RuntimeInstance i_runtimeInstance,
>                             String i_logTag, String i_templateText
>                           )
>   throws ParseException
>   {   this(i_runtimeInstance, i_logTag, new StringReader(i_templateText));
>   }
> 
>   private void render(Writer o_writer, Context i_ctx)
>   throws Exception
>   {   InternalContextAdapterImpl ica = new
> InternalContextAdapterImpl(i_ctx);
>       ica.pushCurrentTemplateName(m_logTag);
>       m_nodeTree.init(ica, m_runtimeInstance);
> 
>       try
>       {   m_nodeTree.render(ica, o_writer);
>       }
>       finally
>       {   ica.popCurrentTemplateName();
>       }
>   }
> 
>   public String render(Context i_ctx)
>   throws Exception
>   {   Writer wrt = new StringWriter();
>       render(wrt, i_ctx);
>       return wrt.toString();
>   }
> }
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"He who throws mud only loses ground." - Fat Albert


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>