Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a map contains a specific key in Freemarker

Tags:

freemarker

In Java:

Map<String, Object> model = new HashMap<>();
Map<String, String> items = new HashMap<>();
items.put("color", "red");
model.put("items", items);

I now want to include a snippet in my rendered template if items contains the key color.

<#if ???? >
   the map contains a key called color
</#if>

What do I replace the ???? with?

like image 395
Steve McLeod Avatar asked Nov 23 '16 18:11

Steve McLeod


People also ask

How do you check if a map contains a key?

util. HashMap. containsKey() method is used to check whether a particular key is being mapped into the HashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.

How do I insert an image into FTL?

FTL just generates text on the server, like HTML, CSS, whatever, then the client (browser) just gets the output. So, assuming you generate HTML with FTL, you put images into it with <img src="..."> . To learn about FreeMarker, go to http://freemarker.org/docs/index.html, of course. Save this answer.


1 Answers

You can use ?? operator like this:

<#if items['color']?? >
   the map contains a key called color
</#if>
like image 61
glw Avatar answered Sep 25 '22 11:09

glw