velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: [jira] Commented: (VELOCITY-444) Allow evaluation of Java expressions that does not return text or value!
Date Thu, 15 Jun 2006 19:50:54 GMT
Why are you setting your Utility class as a property of the VelocityEngine?

you should be putting that into the VelocityContext you merge with the template.

On 6/15/06, Colbert Philippe (JIRA) <jira@apache.org> wrote:
>     [ http://issues.apache.org/jira/browse/VELOCITY-444?page=comments#action_12416385
]
>
> Colbert Philippe commented on VELOCITY-444:
> -------------------------------------------
>
> Thanks for the quick reply.
>
> My macro does not work whether using slient mode or not.   I can't figure out why.  
I am using Velocity 1.4.
>
> I suggest you try to do what I have done.  Cut and paste my Utility class and pass it
into the Velocity engine.   It seems that Velocity does not recognize my Java expressions.
  I can send you the Utility source if you wish.
>
> Can you clarify if Velocity supports Maps or not?   I need a solution to this problem.
>
>
>
>
>
>
>
> > Allow evaluation of Java expressions that does not return text or value!
> > ------------------------------------------------------------------------
> >
> >          Key: VELOCITY-444
> >          URL: http://issues.apache.org/jira/browse/VELOCITY-444
> >      Project: Velocity
> >         Type: Improvement
>
> >   Components: Source
> >     Versions: 1.4
> >  Environment: WindowsXP
> >     Reporter: Colbert Philippe
> >     Priority: Critical
>
> >
> > I have started to use Velocity as a file preprocessor with my own macros in separate
files.   I quickly realized that there are some features missing in Velocity.
> > I want to create Velocity macros with optional parameters, which is essential for
my application.   When a user calls my macros, she/he should not be obliged to pass all the
necessary parameters to my macro.   The macro should be callable with no parameter or with
partial or full set of parameters.   All parameters will have default values.
> > I think the best way to do this is to have a Map (like a java.util.Map as in HashMap).
  As far as I know, Velocity does not support a Map objects directly.   Such object as a Map
has to be created in Java and used from the Velocity macro language.   Therein lies the problem.
> > Velocity does not have a reserved word for evaluating a Java expression without
returning a value or text.   In the documentation, using #set( $result = $myObj.method() ),
method() can have side effects but it must return a value.   I want to be able to call methods
that return nothing (void).   I have tried many things to try circumventing this but nothing
works.  Velocity always outputs the text of the expression without evaluating it.
> > The solution would be to have a reserve construct similar like #set(....) but it
would be called #eval(....).  Such expression #eval(....) would take a Java expression and
would evaluate it without generating any text.   The #eval(....) would be used to set the
value of my Map.
> >     // Utility Java class that creates and keeps HashMap(s)
> >     public static class Utility {
> >         public static final byte MAP_COUNT = 16;
> >         private final Stack<Map<String,String>> maps =
> > new Stack<Map<String,String>>();
> >         public Utility() { this(MAP_COUNT); }
> >         public Utility(int count) {
> >             for(int i=0;i<count;i++) {
> >                 maps.push(new HashMap<String,String>());
> >             }
> >         }
> >         public Map<String,String> createMap() { return maps.pop(); }
> >         public void discardMap(Map<String,String> map) { map.clear(); maps.push(map);
}
> >     }
> >     protected final Utility util = new Utility();
> >     engine.setProperty("util", util); // ...later passed as property of VelocityEngine
> > ##=======================
> > ## Macro to create a Map and use it inside the macro
> > ##=======================
> > #set( $myMap = $util.createMap() )
> > $myMap.size()
> > #$myMap.put("first", "alpha")
> > #$myMap.put("second", "beta")
> > #$myMap.put("third", "kapa")
> > #$myMap.put("fourth", "gamma")
> > #foreach( $myKey in [ "first", "second", "third", "fourth" ] )
> > $myKey    ${myMap.get($myKey)}
> > #end
> > ##=======================
> > ## Output text file ...DOES NOT WORK AS PLANNED!!
> > ##=======================
> > $myMap.size()
> > #$myMap.put("first", "alpha")
> > #$myMap.put("second", "beta")
> > #$myMap.put("third", "kapa")
> > #$myMap.put("fourth", "gamma")
> > first    ${myMap.get($myKey)}
> > second    ${myMap.get($myKey)}
> > third    ${myMap.get($myKey)}
> > fourth    ${myMap.get($myKey)}
>
> --
> This message is automatically generated by JIRA.
> -
> If you think it was sent incorrectly contact one of the administrators:
>    http://issues.apache.org/jira/secure/Administrators.jspa
> -
> For more information on JIRA, see:
>    http://www.atlassian.com/software/jira
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message