Re: macro errors
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, July 25, 2012, 5:42:51 PM, Sam Steingold wrote:
[snip]
> yes. result is a JavaBean in my case, and getTitle() is defined.
>
> are you suggesting that "." denotes both hash table and field access?
If the left-hand-operand is a java.util.Map, then it calls
Map.get(rho), otherwise it reads a JavaBean property or reads a public
Java field. Usually. To be exact, what "." does entirely depends on
the object_wrapper setting, and the type of the object. Like, if you
are using the default object wrapper, for a W3C DOM node it will issue
an XPath query.
> i.e., I could have written scmr.results.model instead of
> scmr.results[model], right?
scmr.results["model"] is the same as scmr.results.model. But
scmr.results[model] is something different (dynamic key).
>> There's also an old implementation bug (that's kept for
>> backward-compatibility), where, if you pass an undefined expression to
>> a macro as a parameter (like even something that's indeed a typo), and
>> the macro defines a default value for the parameter, that default
>> value will kick in. So if you prefer, you can just do this:
>> <#macro sentiment scmr! model><#if scmr?is_sequence>
>> and then you don't need the `!` when calling the macro.
>
> I got
> Encountered "!" at line 2, column 23 in
> com/addthis/sentiment/sentidemo.ftl.
Eh... sorry. I meant:
<#macro sentiment scmr=false model><#if scmr?is_boolean>
> Was expecting one of:
> "=" ...
> "..." ...
> "," ...
> ")" ...
> <ID> ...
> ">" ...
>
> column 23 is "!" in scmr!
>
>
> okay, here is what seems to be working for me:
>
> <#macro sentiment scmr model><#if scmr != ''>
> <#assign senti = scmr.results[model]>
> <#if senti??>
> <td>${senti.sentimentType}</td>
> <td>${senti.score?html}</td>
> <#else><td align="center" colspan="2">none</td>
> </#if>
> </#if></#macro>
>
>
> <@sentiment model=model scmr=result.title! />
> <@sentiment model=model scmr=result.body! />
> <@sentiment model=model scmr=result.mean! />
>
> the big question:
> will the "<#if senti??>" test in the macro work as intended?
That line alone would, but <#assign senti = scmr.results[model]> will
fail if it evaluates to a null. In general, wherever you can get a
null during normal operation, you have to specify a default with "!".
> what is the value of "senti" anyway? a string? the java bean object?
I can't know, since I don't know your data-model. Looking at the macro
I guess it's a JavaBean.
> thanks!
>
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/