Simulate null parameters in Freemarker macros -


i'm building site using freemarker , have started heavily using macros. know in freemarker 2.3 passing null value macro parameter equivalent not passing parameter @ i've created global variable called "null" simulate null checking in macros:

<#assign null="nul" /> 

now in macros can this:

<#maco dosomething param1=null>   <#if param1 != null>     <div>win!</div>   </#if> </#macro> 

the problem comes if want pass parameter isn't scalar. instance, passing list (which in freemarker simplesequence) macro , checking against null keyword yields error:

freemarker.template.templateexception: legal comparisons between 2 numbers, 2 strings, or 2 dates. left hand operand freemarker.template.simplesequence right hand operand freemarker.template.simplescalar

i took @ freemarker code , can see issue (comparisonexpression.istrue()):

if(ltm instanceof templatenumbermodel && rtm instanceof templatenumbermodel) {    ... } else if(ltm instanceof templatedatemodel && rtm instanceof templatedatemodel) {   ... } else if(ltm instanceof templatescalarmodel && rtm instanceof templatescalarmodel) {   ... } else if(ltm instanceof templatebooleanmodel && rtm instanceof templatebooleanmodel) {   ... } // here handle compatibility issues else if(env.isclassiccompatible()) {   ... } else {   throw new templateexception("the legal comparisons...", env); } 

so solution can think of set isclassiccompatible true, think call tostring() on both objects , compare result. however, documentation says relying on old features should rewritten.

my quesion is, there solution doesn't rely on deprecated features?

the null reference design error in freemarker. defining custom null value - string - not idea reasons mention. following constructs should used instead:

  • macro , function parameters can have default value, callers can omit them
  • to check if variable null, should use ?? operator: <#if (name??)>
  • when use variable can null, should use ! operator specify default value: name!"no name"
  • to check if sequence (or string) empty, use ?has_content builtin: <#if (names?has_content)>

you can specify empty sequence default parameter value in macro, , test whether it's empty.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -