Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FreeMarker: Check if map value is null

I have a Map whose key and value and custom classes. The key class is called Position and is instantiated with two int (for example new Position(2, 4).

I got rid of this Position class and converted the map to a SimpleHash in order to use it with Freemarker. I now have a SimpleHash whose key is a String remapping Position values (for example "2 4") and whose value is either null or a Lot (custom) class.

In the template I need to check if the value of a given item in the SimpleMap (passed as map) is either null or a Lot instance.

        <#list mapMinY..mapMaxY as y>
            <tr>
                <#list mapMinX..mapMaxX as x>
                    <td>
                        <div>
                            <!-- Check if map[x + " " + y] is null -->
                            ${x}, ${y}
                        </div>
                    </td>
                </#list>
            </tr>
        </#list>

How to do this?

like image 971
pistacchio Avatar asked Jan 18 '23 00:01

pistacchio


1 Answers

Use the ?? operator.

<#if map[x + " " + y]??>It's not null<#else>It's null or missing</#if>

See also the related part of the Manual.

like image 55
ddekany Avatar answered Jan 27 '23 06:01

ddekany