Freemarker - How to indirectly recognize a missing list -
in freemarker, how can indirectly identify missing list variable? indirectly, mean have string value containing name of list; need convert string variable name (of list) , check see if list present. indirection critical application.
for example, code works:
<#assign existing_list = ["a","b"]> <#assign existing_list_name = "existing_list"> <#assign ref_existing_list = existing_list_name?eval> <#if ref_existing_list?has_content> correctly identified existing list. <#else> failed identify existing list. </#if>
producing output:
correctly identified existing list.
but if list not present, cannot convert string variable name check if list present. example:
<#assign nonexistent_list_name = "nonexistent_list"> <#assign ref_nonexistent_list = nonexistent_list_name?eval> <#if ref_nonexistent_list?has_content> failed identify list non-existent. <#else> correctly identified list non-existent. </#if>
aborts freemarker following error:
<freemarker>[error] freemarker.core.invalidreferenceexception: following has evaluated null or missing: ==> nonexistent_list_name?eval [in template "utilities\\mhc\\templates\\app\\app.h.ftl" @ line 17, column 34]
it seem need capability either convert string variable name if variable missing, or else execute eval on null variable reference without aborting. have not been able find either of freemarker capabilities.
can propose solution?
the key thing realize here can't assign missing things variables. can these:
<#assign nonexistent_list_name = "nonexistent_list"> <#if .vars[nonexistent_list_name]?has_content> list exists , not empty <#else> list doesn't exists or it's empty </#if> <#-- above nicer, still works: --> <#if nonexistent_list_name?eval?has_content> list exists , not empty <#else> list doesn't exists or it's empty </#if>
another thing that's know ?has_content
gives false
existing empty lists. if wanted check if list exists, use ??
instead (like <#if .vars[nonexistent_list_name]??>
). if indeed want treat empty , missing lists same way, then, after all, can assignment too:
<#assign nonexistent_list_name = "nonexistent_list"> <#assign nonexistent_listref = .vars[nonexistent_list_name]!> <#if nonexistent_listref?has_content> list exists , not empty <#else> list doesn't exists or it's empty </#if>
(note #list
can have #else
branch empty lists, can save characters when want listing anyway.)
Comments
Post a Comment