Velocity, how to retrieve a hashmap value using another velocity variable - velocity

I have a HashMap in my bean:
HashMap<String, SomeObject> someHashMap;
Then in the velocity template I need to access the HashMap with a value that I have in velocity from other source (in fact I have many keys not only one that's why I need to get the values this way):
$key
How can I access the hashmap with this key? I'm trying:
$someHashMap.get($key)
and
${someHashMap.get($key)}
But those two only write the same thing to the output, meaning that with the first line I literally get:
$someHashMap.get($key)
In the webpage.
Which is the correct way/syntaxis to do this?
Thanks!

Both are correct syntax, and they should work.
Does $key have the right value? Print it.
Does $someHashMap indeed point to the map? Print it. If not, perhaps you forgot to put in the VelocityContext being used.
Is the value stored under that key null? The default behavior of Velocity is to print out the code that was called when the outcome is null. To make it not do that, use the silent notation: $!{someHashMap.get($key)}

I had this exact same problem. In my case I tried to do this:
$map.get($locale)
where $locale is e.g. "fi_FI". I solved it by adding quotes inside the brackets:
$map.get("$locale")
I'm not sure, but I think the rationale goes like this:
$map.get( $locale ) -> $map.get( fi_FI ) -> Velocity gets confused
$map.get("$locale") -> $map.get("fi_FI") -> Velocity retrieves correct value

Related

Proper Syntax When Using dot Operator in String Interpolation in Dart

In Dart/Flutter, suppose you have an instance a of Class Y.
Class Y has a property, property1.
You want to print that property using string interpolation like so:
print('the thing I want to see in the console is: $a.property1');
But you can't even finish typing that in without getting an error.
The only way I can get it to work is by doing this:
var temp = a.property1;
print ('the thing I want to see in the console is: $temp');
I haven't found the answer online... and me thinks there must be a way to just do it directly without having to create a variable first.
You need to enclose the property in curly braces:
print('the thing I want to see in the console is: ${a.property}');
That will then print the value of a.property.
It seems you can also do this, but it doesn't seem to be documented anywhere:
print('..... $a.$property1');

How to append hash tables in velocity template?

I tried to append two hash tables in velocity.
#foreach($dun1 in $dotcontent.pull("+structureName:Checnas +(conhost:fe1d98e8-9699-4f3f-abf5-a6c0afc8ab47 conhost:SYSTEM_HOST)",10,"modDate desc"))
#set($foo={
$!{dun1.mname}:$!{dun1.subname}
})
#end
In the above for each loop I am pulling content from structure "Checnas".
But at the end we can get only the last value in the content.To solve that we need to append for every iteration.I need syntax for appending hash tables.
Please help me to solve this.
Your code currently is over writing $foo each time and hence you are just getting the last value. You can use lists in velocity to achieve this.
This might work:
#set($listOfMnames=[])
#set($listOfSubNames=[])
#foreach($dun1 in $dotcontent.pull("+structureName:Checnas +(conhost:fe1d98e8-9699-4f3f-abf5-a6c0afc8ab47 conhost:SYSTEM_HOST)",10,"modDate desc"))
#set($foo=$listOfMnames.add($!{dun1.mname}))
#set($foo=$listOfSubNames.add($!{dun1.subname}))
#end
This way, you will end up with two lists 'listOfMnames' and 'listOfSubNames', both fully populated. You can later iterate through them to print/utilise their values.
This link will be helpful and tell you the purpose of using $foo which is not being used and just being assigned.
Alternatively, you can also use velocity maps with proper key/val pairs but be sure to declare it before the loop begins.

Putting the cursor in a specific place in a live template

I just wanted to know how you put a cursor in a specific place in a live template for IntelliJ
For example:
# $var$ is an insance of the $objectType$ class
assert isinstance($var$, $objectType$)$END$
What happens here is that your cursor gets dragged to $var$ in the comment string first and then to your other values inside assert. What I wanted to know is how you chose where the cursor goes first.
I've read the documentation, but this is not mentioned, although a lot of other things are.
You can arrange the order that your variables are visited in. You find the information under bullet number five in this IntelliJ help document: http://www.jetbrains.com/idea/webhelp/creating-and-editing-template-variables.html
To arrange variables in the order you want IntelliJ IDEA to switch between associated input fields, use the Move Up and Move Down buttons.
Edit
You have to update the macro definition to similar to this:
# $varComment$ is an insance of the $objectTypeComment$ class
assert isinstance($var$, $objectType$)$END$
And then you define the order and expression to something like this (I didn't have any good expression for the var and orderType for you):
Since you fill in the Skip if defined for the two comment variable they will just take the values from the var and orderType and fill it in. This will do exactly what you are looking for :-)

Velocity Template engine - key-value-map

I have some problems wo use a key-value-map into Velocity.
Someone has an example of this functionality?
$myMap ={}
$myMap.put("mykey1", "myvalue")
$myMap.delete("mykey1")
$myMap.getValue("mykey1")
As Nathan said, you should use:
#set ($myMap = {})
to create a new map and assign it to a variable.
Now, why is the put call printed.
Anything that is not inside a directive, like #set(not printed) or #if(not printed) or #foreach(again not printed), is printed, including free text, variables, and method calls.
Velocity can't distinguish between the different semantics of $myMap.get('mykey') and $myMap.put('key', 'value') (reader vs. writer), so the result of the put call is printed, just like any other method.
Whenever something can't be properly evaluated, because a variable is not defined or somewhere along the line a method returns null, the code that failed to be evaluated is dumped literally into the output.
As the documentation of the put method states, the function returns the previous value stored for that key, or null if no value was set at all.
Summing it all up, it's normal to get that line printed.
To try this theory out, you can do this:
#set ($myMap = {})
$myMap.put('key', 'first value')
$myMap.put('key', 'second value')
$myMap.get('key')
This will be printed:
$myMap.put('key', 'first value')
first value
second value
There are two things you can do so that the line isn't printed:
Store the outcome of the function in a temporary variable: #set ($discard = $myMap.put('key', 'value')
Use the silent method call: $!myMap.put('key', 'value')
I'd recommend the first one, since the second one will still print something when you're replacing an existing value.
Did you try doing:
#set( $myMap = {} )
Also, make sure you are using a modern version of Velocity. Ancient ones did not have map syntax in VTL.
Just add ! to not print put:
$!myMap.put('key', 'second value')

SWI prolog make set of variables name with rbtrees or others means

I have got a term from which I want to get set of variables name.
Eg. input: my_m(aa,b,B,C,max(D,C),D)
output: [B,C,D] (no need to be ordered as order of appearance in input)
(That would call like set_variable_name(Input,Output).)
I can simply get [B,C,D,C,D] from the input, but don't know how to implement set (only one appearance in output). I've tried something like storing in rbtrees but that failed, because of
only_one([],T,T) :- !.
only_one([X|XS],B,C) :- rb_in(X,X,B), !, only_one(XS,B,C).
only_one([X|XS],B,C) :- rb_insert(B,X,X,U), only_one(XS,U,C).
it returns tree with only one node and unification like B=C, C=D.... I think I get it why - because of unification of X while questioning rb_in(..).
So, how to store only once that name of variable? Or is that fundamentally wrong idea because we are using logic programming? If you want to know why I need this, it's because we are asked to implement A* algorithm in Prolog and this is one part of making search space.
You can use sort/2, which also removes duplicates.