In Freemarker is it possible to use the value of a hash item as a method? i.e., ${key}: ${val?val} - freemarker

I'm trying to make a function or macro to test a variable against all Freemarker is_xxxx types (https://freemarker.apache.org/docs/ref_builtins_expert.html#ref_builtin_isType), so that I can just run a variable through that function/macro to see what it is.
I could obviously just run each one at a time and return that, i.e.,
is_string? ${var?is_string?c} <br/>
is_hash? ${var?is_hash?c} <br/>
I was hoping to avoid that by <#list> ing through a hash, but I couldn't figure it out ... then I just got curious if this sort of thing were even possible.
At a loss how to do this, or if I can.
For example
<#assign builtIns = {"is_string":"is_string","is_number":"is_number","is_boolean":"is_boolean","is_date_like":"is_date_like","is_date_only":"is_date_only","is_time time":"is_time time",
"is_datetime":"is_datetime","is_unknown_date_like":"is_unknown_date_like","is_method":"is_method","is_transform":"is_transform","is_macro":"is_macro","is_hash hash":"is_hash hash","is_hash_ex":"is_hash_ex","is_sequence
sequence":"is_sequence sequence","is_collection":"is_collection","is_collection_ex":"is_collection_ex","is_enumerable":"is_enumerable","is_indexable":"is_indexable","is_directive":"is_directive","is_node
node":"is_node node","is_markup_output":"is_markup_output"}
/>
<#list builtIns as key,val>
${key},${val?val} <br/> // or something like this.
</#list>
Make sense? Possible? A different way to do this entirely?
Thanks!

No, the so called built-ins (things after the ?) has no value, so you can't put them into variables. That's unlike normal methods, FTL macros, and FTL functions, which can be passed around as a value. The difference is because some built-ins affect template parsing (kind of like compilation), so they are not purely runtime.
Well, you could get around that with generating the expression as a string then do myExpressionString?eval, but that's awkward and somewhat slow.

Related

How tu use a variable in if in ftl?

I am learning ftl and have problem with variables.
I have an element a which has its own subelement b.
I access the subelement with the following method:
a.getChild("b")
Now, I check whether a subelement has content in the following way:
<#if a.getChild("b").getData()?has_content>
and this works as expected.
Now, I wanted to shorten this if's syntax by doing the following:
<#assign b>${a.getChild("b")}</#assign>
<#if b.getData()?has_content>
However, this produces error:
For "." left-hand operand: Expected a hash, but this has evaluated to a string
What am I doing wrong and how do I eliminate the need for calling the getChild in ifs?
I have hundreds of ifs in my production environment which all use getChild, sometimes even multiple times if subelements are nested so it would be great if I could shorten the syntax by assigning a child element to a variable.
I just started learning ftl yesterday, so this might be stupid question, sorry.
Simply do <#assign b = a.getChild("b")>.
With ${a.getChild("b")} you've outputted a string which then was assigned.
See the documentation also: https://freemarker.apache.org/docs/ref_directive_assign.html

Refactoring Business Rule, Function Naming, Width, Height, Position X & Y

I am refactoring some business rule functions to provide a more generic version of the function.
The functions I am refactoring are:
DetermineWindowWidth
DetermineWindowHeight
DetermineWindowPositionX
DetermineWindowPositionY
All of them do string parsing, as it is a string parsing business rules engine.
My question is what would be a good name for the newly refactored function?
Obviously I want to shy away from a function name like:
DetermineWindowWidthHeightPositionXPositionY
I mean that would work, but it seems unnecessarily long when it could be something like:
DetermineWindowMoniker or something to that effect.
Function objective: Parse an input string like 1280x1024 or 200,100 and return either the first or second number. The use case is for data-driving test automation of a web browser window, but this should be irrelevant to the answer.
Question objective: I have the code to do this, so my question is not about code, but just the function name. Any ideas?
There are too little details, you should have specified at least the parameters and returns of the functions.
Have I understood correctly that you use strings of the format NxN for sizes and N,N for positions?
And that this generic function will have to parse both (and nothing else), and will return either the first or second part depending on a parameter of the function?
And that you'll then keep the various DetermineWindow* functions but make them all call this generic function?
If so:
Without knowing what parameters the generic function has it's even harder to help, but it's most likely impossible to give it a simple name.
Not all batches of code can be described by a simple name.
You'll most likely need to use a different construction if you want to have clear names. Here's an idea, in pseudo code:
ParseSize(string, outWidth, outHeight) {
ParsePair(string, "x", outWidht, outHeight)
}
ParsePosition(string, outX, outY) {
ParsePair(string, ",", outX, outY)
}
ParsePair(string, separator, outFirstItem, outSecondItem) {
...
}
And the various DetermineWindow would call ParseSize or ParsePosition.
You could also use just ParsePair, directly, but I thinks it's cleaner to have the two other functions in the middle.
Objects
Note that you'd probably get cleaner code by using objects rather than strings (a Size and a Position one, and probably a Pair one too).
The ParsePair code (adapted appropriately) would be included in a constructor or factory method that gives you a Pair out of a string.
---
Of course you can give other names to the various functions, objects and parameters, here I used the first that came to my mind.
It seems this question-answer provides a good starting point to answer this question:
Appropriate name for container of position, size, angle
A search on www.thesaurus.com for "Property" gives some interesting possible answers that provide enough meaningful context to the usage:
Aspect
Character
Characteristic
Trait
Virtue
Property
Quality
Attribute
Differentia
Frame
Constituent
I think ConstituentProperty is probably the most apt.

Is there a more elegant way to check a variable in freemarker?

I have objects and want to check if a certain property exists.
I have nested maps and lists that look like this:
some.videos.files (map including a map including a list)
some always exists but may be empty.
If I do this:
<#if some.videos.files?has_content>
and some.videos doesn't exist I get an error.
So I do this now:
<#if some.videos?has_content && some.videos.files?has_content>
Is there a more elegant way to do this?
I know I could do
<#if some.videos?? && some.videos.files?has_content>
but I don't consider this to be much better especially if you may have deeper nested variables.
Basically I only want to know if some.videos.files exists and is not empty.
Yes, (some.videos.files)?has_content. The same logic works with ?? and ! too, like (some.videos.files)?? and (some.videos.files)![].

Function inputs and outputs in PROLOG

I'm building some relatively simple functions in PROLOG that take one input and one output. For simplicity, something like
func(List, Item, [Item | List]).
Now, I've got code that will call several of these functions in a row and pass the result on. The issue is that I have to keep creating new variable names for all of the outputs.
someOtherFunc(List, Item) :-
func(List, Item, Output1),
doSomething(Output1).
The issue here is that I actually have several func and several doSomething and would really appreciate not having to bind an Output1 variable explicitly. Is there any way to achieve this?
I'm not sure about what you're asking, but first of all please note that those are not functions, but predicates. This is a totally different programming paradigm. Variables are not "boxes" where you put in and out some data: they're closer to the mathematical meaning of variable, since once you bind them to some constraints on their values it's forever.
To go back to your question, the answer is no, you can't avoid binding some Output1 like that. Sometimes you can put in an underscore to tell prolog you just don't care about that value, but doing so you lose the ability to make use of that particular value. In your example you would like to do something like this (in a imperative pseudocode):
var list = ..., item = ...;
doSomething(func(list, item));
There's no other way in prolog as far as I know, you just have to use intermediate variables as you did. The only improvement I can suggest, is to choose very carefully predicates and variables names.
func1(Input1, Input2) :-
func2(Input1, Input2, Output1),
useFun(Output1, Output2).
/* Output2 the result I obtain from the function useFun */

test if string can be converted to a number in FreeMarker

I am trying to test whether a string can be converted into a number in FreeMarker. For example, "123" and "3.14" can be converted, but "foo" can't. I know that I can test for this by using the number method on the string (e.g. "123"?number) and seeing whether it produces an error, but I am looking for a way to test for this without causing an error.
I tried ?matches("^\d+$"), and it works fine for integers, but I am looking for something that works on all numbers.
I can probably do it using a more sophisticated regex, but I am wondering if there is a simpler way.
The simpler way is to not do it in FreeMarker :-) This sounds like something controller (or method on model) should be doing rather than view template. That said, you have a few options:
Use ?number built-in within <#attempt> / <#recover> block.
Write a method in one of your model objects to check whether your string into a number and invoke it from the template.
Write a custom directive to do this for you.

Resources