Velocity
  1. Velocity
  2. VELOCITY-6

#if in #foreach seems to be caching results in Velocity 1.1 rc1

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Incomplete
    • Affects Version/s: 1.0b3
    • Fix Version/s: None
    • Component/s: Build
    • Labels:
      None
    • Environment:
      Operating System: Linux
      Platform: PC

      Description

      Using this code:

      --8<--

      import java.io.*;
      import java.util.*;

      import org.apache.velocity.VelocityContext;
      import org.apache.velocity.Template;
      import org.apache.velocity.exception.*;
      import org.apache.velocity.app.Velocity;

      public class VelTest {

      public static void main(String[] args) {

      Template template = null;

      try

      { Properties props = new Properties(); props.setProperty("file.resource.loader.path", args[0]); Velocity.init(props); VelocityContext context = new VelocityContext(); HashMap map = new HashMap(); map.put("wonka", "werfl"); map.put("wertheimer", "wiffen"); map.put("wicken", "witch"); map.put("wooble", new Integer(3767)); map.put("wablle", new Long(System.currentTimeMillis())); context.put("name", map); context.put("nameKeys", map.keySet()); template = Velocity.getTemplate(args[1]); StringWriter sw = new StringWriter(); template.merge(context, sw); System.out.println("template output: "+sw); }

      catch(ResourceNotFoundException rnfe)

      { System.out.println("Can't find your template!"); }

      catch(ParseErrorException pee)

      { System.out.println("Your template has a syntax error: "+pee); }

      catch(Exception e)

      { System.out.println("Something bad happened: "+e); }

      }
      }

      --8<--

      And using this template:

      --8<--

      <HTML>
      <BODY>
      Hello. This is about the object \$name whose value is $name
      \$name is actually a $name.getClass().getName()
      Name is $name.size() in size

      <ul>
      #foreach ($element in $nameKeys)
      <li>key=$element value=$name.get($element)</li>
      #if ($name.get($element).getClass().getName().equals("java.lang.String"))
      The value is a String type
      #else if
      ($name.get($element).getClass().getName().equals("java.lang.Integer"))
      The value is an Integer type
      #end
      #end
      </ul>
      </BODY>
      </HTML>

      --8<--

      I get this output:

      --8<--

      <HTML>
      <BODY>
      Hello. This is about the object $name whose value is

      {wicken=witch, wonka=werfl, wooble=3767, wablle=990844112188, wertheimer=wiffen}

      $name is actually a java.util.HashMap
      Name is 5 in size

      <ul>
      <li>key=wicken value=witch</li>
      The value is a String type
      <li>key=wonka value=werfl</li>
      The value is a String type
      <li>key=wooble value=3767</li>
      if (true)
      The value is an Integer type
      <li>key=wablle value=990844112188</li>
      if (false)
      The value is an Integer type
      <li>key=wertheimer value=wiffen</li>
      The value is a String type
      </ul>
      </BODY>
      </HTML>

      --8<--

      Notice the weird "if(false)" diddly-doos in there.

        Activity

        No work has yet been logged on this issue.

          People

          • Assignee:
            Unassigned
            Reporter:
            Nick Bauman
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development