Re: macro errors

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Wednesday, July 25, 2012, 11:09:59 PM, Sam Steingold wrote:

>> * Daniel Dekany <[email protected]> [2012-07-25 22:59:46 +0200]:
>>
>>>
>>> <#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 "!".
>
> you mean it should be
>
> <#assign senti = scmr.results[model]! />
>
> right?

Only if the default value that you want is empty string + empty
sequence + empty hash (a multi-type value). See:
http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing

Also note that if you have a default value, then <#if senti??> will
never be executed, of course.

>>> 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.
>
> what I meant was whether it was a java object or an html/freemarker string.

Assuming it's a JavaBean in Java, it's a hash (+ string) in the
type-system of FreeMarker. Assuming you are using the default object
wrapper at least.

> 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/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.