diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java index d5cbb14cd..a6ec09833 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java @@ -37,7 +37,8 @@ public class TestController extends FreeMarkerHttpServlet { body.put("now", now); // In template: ${now?date}, ${now?datetime}, ${now?time} - // You can add to a collection AFTER putting it in the template data model + // You can add to a collection AFTER putting it in the template data model. + // The data model contains a reference to the collection, not a copy. List fruit = new ArrayList(); fruit.add("apples"); fruit.add("bananas"); @@ -49,12 +50,31 @@ public class TestController extends FreeMarkerHttpServlet { String animal = "elephant"; body.put("animal", animal); animal = "camel"; - + + // Because the data model contains a reference to the collection, changing + // one also changes the other. + List animals = new ArrayList(); + animals.add("elephant"); + animals.add("tiger"); + Map zoo1 = new HashMap(); + Map zoo2 = new HashMap(); + zoo1.put("animals", animals); + zoo2.put("animals", animals); + zoo1.get("animals").add("monkey"); + body.put("zoo1", zoo1); + body.put("zoo2", zoo2); + + getBerries(body); + // Create the template to see the examples live. String bodyTemplate = "test.ftl"; return mergeBodyToTemplate(bodyTemplate, body); } + + private void getBerries(Map body) { + body.put("berries", "strawberries, raspberries, blueberries"); + } } diff --git a/webapp/web/templates/freemarker/body/test.ftl b/webapp/web/templates/freemarker/body/test.ftl index 0372b012b..dce4cb455 100644 --- a/webapp/web/templates/freemarker/body/test.ftl +++ b/webapp/web/templates/freemarker/body/test.ftl @@ -25,6 +25,22 @@

Animal: ${animal}

+

Zoo 1

+
    +<#list zoo1.animals as animal> +
  • ${animal}
  • + +
+ +

Zoo 2

+
    +<#list zoo2.animals as animal> +
  • ${animal}
  • + +
+ +

Berries: ${berries}

+ <@dump var="now" /> <@dump var="urls" /> <@dump var="fruit" />