Re: Simulating null parameters in macros
"Newman, John W" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <721876F56F42304A8CB1CCFF9BFC09BB128CF69A@MSXMBXNSPRD09.acct.upmchs.net> |
Typically we just default it to an empty collection [], object {}, or string "" and use the?has_content built in to check that there's something actually in there. You can use ?exists to differentiate between null vs empty, but I think what you're getting at is it's really not possible to pass null in and have it remain null past the default assignment.
[#macro printList theList=[]]
[#if theList?hasContent]
<ul>
[#list theList as item]
<li>${item.name}</li>
[/#if]
</ul>
[#elseif ! theList?exists]
I don't think you'll be able to get this branch to run ever, since removing the default and calling with null will fail. You win the prize.
[#else]
Empty but non-null list
[/#if]
[/#macro]
[@printList call.a.java.method.that.returns.a.collection.or.possibly.null() /] [#-- will default to [] and print empty but non-null list--]
[#assign testList = [] /]
[@printList testList /]
[#assign testList = testList + [1, 2] /]
[@printList testList /]
AFAIK, there's really no way to get that branch to run, unless you do sort of what you were doing with some magic default value and have null grab that instead and check against it. Eh... I hate doing things like that. What the heck are you supposed to use that your users couldn't possibly pass in? some object {with: "some key maybe" } ...
In general though, I like the way freemarker treats null. It forces you to be explicit and helps to trap errors instead of going on silently with nothing. There ought to be a way to handle this case however, there probably is..
-John
From: Hinkle, Cameron [mailto:[email protected]]
Sent: Tuesday, July 05, 2011 12:22 PM
To: [email protected]
Subject: [FreeMarker-user] Simulating null parameters in macros
I asked this question on Stack Overflow but no one seemed to have an answer for me. Hoping someone here will:
http://stackoverflow.com/questions/6553004/simulate-null-parameters-in-freemarker-macros
I'm building a site using Freemarker and have started heavily using macros. I know in Freemarker 2.3 that passing a null value into a macro as a parameter is equivalent to not passing a parameter at all so I've created a global variable called "null" to simulate null checking in my macros:
<#assign null="NUL" />
Now in my macros I can do this:
<#maco doSomething param1=null>
<#if param1 != null>
<div>WIN!</div>
</#if>
</#macro>
The problem comes if I want to pass a parameter that isn't a scalar. For instance, passing a List (which in Freemarker is a SimpleSequence) to a macro and checking against my null keyword yields the error:
freemarker.template.TemplateException: The only legal comparisons are between two numbers, two strings, or two dates. Left hand operand is a freemarker.template.SimpleSequence Right hand operand is a freemarker.template.SimpleScalar
I took a look at the freemarker code and I can see the 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 we handle compatibility issues
else if(env.isClassicCompatible()) {
...
}
else {
throw new TemplateException("The only legal comparisons...", env);
}
So the only solution I can think of is to set isClassicCompatible to true, which I think will call toString() on both objects and compare the result. However, the documentation specifically says anything relying on old features should be rewritten.
My quesion is, is there a solution to this that doesn't rely on deprecated features?
Thanks,
Cameron
Cameron Hinkle | Senior Application Engineer | Nike DCIT | 503.532.3075 | 7. Evolve Immediately |
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user