You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by "Nathan Bubna (JIRA)" <ji...@apache.org> on 2006/06/15 20:39:30 UTC

[jira] Resolved: (VELOCITY-444) Allow evaluation of Java expressions that does not return text or value!

     [ http://issues.apache.org/jira/browse/VELOCITY-444?page=all ]
     
Nathan Bubna resolved VELOCITY-444:
-----------------------------------

    Resolution: Invalid

Hi Colbert,

If you do not want to see method calls or references that have or return no value in your output, you just need to use the silent notation:

$!map.put('foo', 'bar')

When you do $map.put('foo', 'bar') it *does* get executed, but since it returns no value, Velocity does what it always does in such situations and prints the full reference (i.e. "$map.put('foo', 'bar')" ) in the output after it evaluates it.

Also, Velocity 1.5-dev does have native syntax for creating maps in your templates.

Your report does make me wonder why we don't support #set( $map.foo = 'bar' ) type syntax for populating maps.  Actually, i've never tried it.  Perhaps it already is supported :), but if not, that's something we should consider.

cheers,
nate

> 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