Freemarker: Expression is undefined - freemarker

I have this code:
[#list doc.presidents as president][#if president.name?has_content]${president.name}[/#if][/#list]
When I run it, I have the error: Expression president is undefined.
What am I doing wrong?
Thanks!

I suppose you have a null in that presidents list.
As of the nested content of #list, it could be just written as ${president.name!}. If you expect the president itself to be null, then ${(president.name)!}.

Related

Problem detecting and using a nullable value in Freemarker

I have a POJO object that I have serialized from JSON (in Java). I am using an object wrapper constructed via:
DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_27);
builder.setExposeFields(true);
objectWrapper = builder.build();
I use the setExposeFields(true) because the object I am wrapping is not a Java bean, but rather just a POJO that contains public fields.
I am doing the following in my template:
<#ConditionOccurrence co = c/>
<#macro ConditionOccurrence co>
<#list co?keys as key>
${key}
</#list>
${co.occurrenceStartDate!'wtf'}
${co["occurrenceStartDate"]}
A condition occurrence of: ${codesetName(co.codesetId, "any condition")}
<#if co.first!false>- for the first time in the person's history</#if>
<#if (co["occurrenceStartDate"])??>co.OSD is null: </#if>
</#macro>
Note, the 'c' is an element in a sequence, and is not important to the exact problem I am having.
The output of the template shows this:
stopReason
getClass
gender
CorrelatedCriteria
providerSpecialty
occurrenceStartDate
occurrenceEndDate
visitType
accept
codesetId
hashCode
conditionSourceConcept
equals
conditionType
toString
conditionTypeExclude
class
first
age
org.ohdsi.circe.cohortdefinition.DateRange#68e62ca4
org.ohdsi.circe.cohortdefinition.DateRange#68e62ca4
A condition occurrence of: Psoriasis
- for the first time in the person's history
co.OSD is null:
The first set of lines are all the keys in my POJO. This is correct.
the two lines of output:
org.ohdsi.circe.cohortdefinition.DateRange#68e62ca4
org.ohdsi.circe.cohortdefinition.DateRange#68e62ca4
This is showing that the field occurrenceStartDate is an object of type DateRange. note this could be null in some cases, so I am checking how to check for null...
The next part of the output:
- for the first time in the person's history
co.OSD is null:
This is showing that it is reading the 'first' attribute of the object correctly, and I have switched the raw JSON from 'true' to 'false' and the template responds properly to the change in this value. Note, in the object, the 'first' field is type Boolean.
The second line: co.OSD is null is what is confounding me. I confirmed earlier that outputting the 'occurrenceStartDate' field shows that it holds a DateRange object. But, this statement is evaluating to TRUE (ie: it is null):
#if (co["occurrenceStartDate"])??>co.OSD is null: </#if>
I have tried with both dot notation and bracket notation. For some reason, the ?? operator on that field is saying it is null. Note, the underlying object isn't a simple String or Number type, it is a simple POJO class DateRange with 3 String properties on it. Again, these are not JavaBeans, these are just POJOs.
Can anyone explain why the ?? operator says it is empty when it is clearly referencing an object? Btw: if I attempt to access co.occurrenceStartDate at all, it results in a template error that I'm referencing a null value, so the core problem here is why does the wrapper thing it is a null?
Thank you in advance for your help.
The ?? operator means "is present", not "is missing". So your line should be:
<#if !(co.occurrenceStartDate??)>co.OSD is null: </#if>

handle null recursively in freemarker

I want to execute eval on a string "bean.data.id", but "bean.data" is null. is there any way to make this expression do not throw exception? or what is the right way to handle this? Thanks a lot!
${name?eval}
name = 'bean.data.id'
bean = {"data": null}
You could do this (assuming printing nothing on null is fine):
${'(${name})!'?eval}

linq expression for string array always results in "The name 'result' does not exist in the current context"

I'm trying to get certain keys from the Request.Form.AllKeys string array by using the following:
var result = keys.Where(key => key.StartsWith("added"));
The result is "The name 'result' does not exist in the current context" no matter what I do.
I've also tried:
var result = (from keys in Request.Form.AllKeys
where keys.StartsWith("added")
select keys).ToArray();
Same thing.
I'm new to Linq and Lambda expressions and all, so please forgive the ignorance.
Regards,
Jacques
I found the answer to my question: Delayed execution.
When I actually executed the code and then followed it up by using result.Any() the expression was executed and turned out the correct results.

Access elements by index in an FTL Template

Need to access 1st and 2nd element of a list in the template.
My Java code:
myMap.put("key", Arrays.asList("val1", "val2");
My FTL Template:
<#list myMap?keys as key>
${myMap[key][0]}, ${myMap[key][1]}
<-- the line above fails with undefined expression on myMap[key][0]. I checked and myMap[key] is a SimpleSequence. Also, tried ${myMap[key]?first} and that failed with the same error. Any ideas?
[0] and [1] are fine for this, but it looks like that either the sequence has 0 elements, or those elements are null. What does ${myMap[key]?size} print? BTW, you can write ${myMap[key][0]!'some default'} if you want to get a value even if the item is non-existant or null.
Your problem is that you put the List into your 'myMap' object with the key: "key" then try and access it with they key: "keys".
This is why you were getting an undefined expression, to correct it:
<#list myMap?key as k>
${myMap[k][0]}, ${myMap[k][1]}
or of course you could change your java code to
myMap.put("keys", Arrays.asList("val1", "val2");
and use the ftl code as is.

LINQ, "Value cannot be null", yet query evaluates

I have the following linq query:
var test = vendorContact.vendorContactItem
.Where(x => x.ItemNumber == vendorContactItem.Item_Number)
.FirstOrDefault();
It fails on this piece of code, "Value cannot be null, parameter name: source" ... yet it also displays, in the local variables window, "test" as a variable with all its properties populated.
vendorContact.VendorContactItem is null. Presumably this would be the first element to be added to the list. So how is "test" evaluating correctly while simultaneously throwing up that error?
I'm new to Linq, so excuse me if this is an obvious question.
If this is in a loop test in the locals window contains the last value of test, from the last iteration of the loop.
Edit: This has really nothing to do with LINQ, but how the debugger works.

Resources