Dynamic Freemarker variable name - variables

I'm trying to set a variable with a dynamic name. This means the name for my new variable comes from another variable:
<#-- in real world I wouldn't declare this variables right here -
they would come from somewhere else -->
<#assign varName = "myVarName"/>
<#assign varValue = "myVarValue/>
<#... set the variable .../>
So that the value can be referenced as follows:
${myVarName} <#-- prints "myVarValue" -->
In a Java directive I would use
Environment#setVariable(String name, TemplateModel model)
to achieve this. But is there a possibility to achieve this with Freemarker directly?

I had a similar problem and Special Variable Reference page helped me:
vars: Expression .vars.foo returns the same variable as expression foo. It's useful if for some reasons you have to use square bracket syntax, since that works only for hash sub variables, so you need an artificial parent hash. For example, to read a top-level variable that has a strange name that would confuse FreeMarker, you can write .vars["A strange name!"]. Or, to access a top-level variable with dynamic name given with variable varName you can write .vars[varName]. Note that the hash returned by .vars does not support ?keys and ?values.
In my case I had to use only strings in my model. I had a bunch of names like Name1, Name2, ... Name10. To make a table of these names I used this code:
<#list 1..10 as x>
<#if .vars["Name" + x]??>
<tr>
<td align="center">${.vars["Name" + x]}</td>
</tr>
</#if>
</#list>

Use a hash. That is, use the name of the variable as the key of the hash.

There's no directive that assigns to a variable that has a dynamic name. But here's a hack to achieve that:
<#'<#assign ${varName} = varValue>'?interpret />
This isn't terribly fast though. It involves FTL parsing each time it's evaluated.

I guess you can do it like this:
${myVarName?eval} <#-- prints "myVarValue" -->
I found the answer from here, and it works to me.

Related

using v-bind:href to bind an expression

I am trying to bind an expression to a URL using v-bind of vue.js.
The binder expression will be a year of the folder and I want that expression to render in the links.
I am trying to get this to bind correctly.
Can someone see what I am improperly concatenating?
<a v-bind:href="'https://www.exmaple.com'+{{$route.params.year | year}}'+'&FolderCTID=0x012000507B97BC3FFDCE4D854E'">My Link</a>
I think you're looking for this:
<a v-bind:href="'https://www.example.com/' + ($route.params.year || year) + '&FolderCTID=0x012000507B97BC3FFDCE4D854E'">My Link</a>
Key changes:
|| instead of |. The latter is a bitwise operator.
Added parentheses to ensure the || is applied correctly. Otherwise the + operators will be applied first.
Ditched the {{ ... }}. They shouldn't appear within v-bind expressions.
Removed the extra ' after the }}.
Added an extra / after the .com.
This will give, for example:
https://www.example.com/2019&FolderCTID=0x012000507B97BC3FFDCE4D854E
It seems likely you'd want a ? in there somewhere but that wasn't included in the original code.
You haven't mentioned what year is but it appears that it's a default for when the year route param is missing. That's certainly what I've assumed.
You may also want to consider using a computed property for this as it's a little complicated for template logic.
Update:
Based on the comment it seems that you intended | to be a Vue filter.
A filter can only be applied to the end of the whole expression. You cannot use it in the middle. If you try to use it in the middle it will just be interpreted as the JavaScript | operator and not as a filter.
I suggest using a method instead. If you have a method called formatYear it can be called using formatYear($route.params.year) within your v-bind expression.
I added this into my hyperlink and it works like a charm! I am sure someone will need this
+ $options.filters.year($route.params.year) +'

How to pass key as variable to template in Bootstrap-Vue tables

There is a new syntax for scoped field slots in tables, see https://bootstrap-vue.js.org/docs/components/table#scoped-field-slots , which looks like
<template v-slot:cell(myColumn)="data">
...
where myColumn is interpreted as a string - key of field from fields to display in our table.
How can I use a variable instead of string? Let's say something like:
let myColumnName = "myColumn";
<template v-slot:cell(myColumnName)="data">
...
When using the new v-slot syntax of Vue 2.6.x, you can use the dynamic slot name syntax.
<template v-slot:[`cell(${myColumnName})`]="data">
or set the full slot name in a variable:
let fieldName = 'myColumn'
let slotName = `cell(${fiedlName})`
<template v-slot:[slotName]="data">
Anything between the square brackets is interpreted as a javascript expression. Just note that you cannot have spaces in the expression (HTML attribute names cannot have spaces).
The second example above is your best bet when using DOM templates. Just note that your variable names should probably be lower cased when using DOM templates (the browser lower cases everything before the = when it parses the template).
If using Single File Components (SFC), then you do not need to worry about letter casing.
To pass in variables you gotta add square brackets around the entire thing.
v-slot:[`cell(${myColumnName})`]="data"
or (whichever you prefer)
v-slot:['cell('+myColumnName+')']="data"

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

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

How to add text plus the text written from a Parameter type C in ABAP?

I am working in an ABAP program and I have a question.
For example in C# when we have a String variable: string name; , and we want this to be filled with some data from a textbox but also add some ohter text.
For example:
string name = "Hello: " + textBox1.text;,
And I want to ask you how can I do this in ABAP ??? How to add text plus the text written from a Parameter type C?
CONCATENATE and the concatenate operator && will do it as answered by Jagger and vwegert. To do it with string expressions, you use the below where name is the screen field or whatever that has the name in it (it doesn't need to be a field-symbol):
greeting = |Hello: { <name> }|.
String expressions are extremely useful as they can be used to build up complex values without declaring extra variables - e.g. they can passed as directly as function module or method parameters without first assigning to a local variable.
You can either use the CONCATENATE keyword or -- in newer releases -- string expressions. Be sure to check the online documentation and sample programs available using the transaction ABAPDOCU, it will save you a ton of seemingly basic questions.
The equivalent operator is &&.
So in your case it would be:
name = 'Hello: ' && textBox1->text.

ColdFusion structkey starting with number

Why does this fail:
<CFIF isdefined("URL.3dfile")>...</CFIF>
with following message:
Parameter 1 of function IsDefined, which is now URL.3dfile, must be a syntactically valid variable name.
and this won't:
<CFIF structkeyexists(URL,"3dfile")>...</CFIF>
Is the way it get's parsed not much the same? And .. are variables starting with numbers invalid or aren't they?
Seybsen - variables names should not begin with a number. This is likely a legacy of older non-java version of CF Where a variable was not part of an object.
However, in the java world everything IS an object. This leads to a syntactical nuance. If you are using variable names in dotted notation your var name will likely throw an error. But use it in other ways and it will succeed.
So this sort of syntax works
url['33foo']
But setting a variable name directly - 33foo = true - will not work.
Here's a post with a full explanation.
http://www.coldfusionmuse.com/index.cfm/2005/9/8/isdefined%20vs%20structkeyexists