Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific map values from inside Freemarker template

I need to pass a Map<String,String> into a Freemarker template, and then, inside the template, cherry pick which values I pluck from the injected map. So something like:

// This map will have a 'fizz' key with a String value of 'buzz'.
Map<String,String> mymap = getSomehow();

Configuration cfg = new Configuration();
Template template = cfg.getTemplate("mytempl.ftl");
StringWriter sw = new StringWriter();

template.process(mymap, sw);

And then, the template (mytempl.ftl):

<h1>${mymap[fizz]}</h1>

But when I run this, I get:

FreeMarker template error: The following has evaluated to null or missing: ==> mymap

Any ideas where I'm going awry?

like image 433
smeeb Avatar asked Nov 29 '25 14:11

smeeb


1 Answers

The first parameter to the process Method is the "context" of the Template. So to pick specific values from the Map you can just use <h1>${fizz}</h1>.

like image 54
Martin Schneider Avatar answered Dec 01 '25 04:12

Martin Schneider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!