How can I use variable names other than $it$ in a StringTemplate partial? - stringtemplate

How can I use variable names other than $it$ in a StringTemplate partial?

What's a partial?
anyway, if template has one arg the ST sets its value too
$a:t()$
for def
t(v) ::= "$v$"
is same as
t() ::= "$it$"

$it$
Here is an example rendered using the default $it$ variable name.
$["FOO","BAR"]:{<h1>$it$</h1>}$
$x$
In this example, the variable is named $x$
$["FOO","BAR"]:{x|<h1>$x$</h1>}$

Related

Can't use $refs using variable name instead of direct value

I'm trying to call a child component's method from my parent component using $refs. It works if I do it the regular way: this.$refs.actualNameOfTheChildComponent.someMethod()
But for my app's needs I have to use a variable name instead of the child component's actual name:
const previousAction = `action${i-1}`
this.$refs.previousAction.showConflict()
In the second line, Vue doesn't understand that I'm not referring to a child component named previousAction, but I'm referring to a child component whose name is the value of the variable previousAction.
What's the right way to do it?
Writing .something is just syntactic sugar for ["something"]. Therefore you can use:
const previousAction = `action${i-1}`
this.$refs[previousAction].showConflict()
You can read more about the differences between dot notation and bracket notation here.
There are some important differences between dot and bracket
notation:
Dot notation:
Property identifies can only be alphanumeric
(and _ and $)
Property identifiers cannot start with a number.
Property identifiers cannot contain variables.
OK — obj.prop_1,
obj.prop$
Not OK — obj.1prop, obj.prop name
Bracket notation:
Property identifiers have to be a String or a variable that references a String.
It is okay to use variables, spaces, and Strings that start
with numbers
OK — obj["1prop"], obj["prop name"]

How to assign a variable in MVEL Template

I would like to assign variables in an mvel template.
Assuming that my template has only a property foo defined, I would like to do the following:
#{bar=foo}
#{bar.name}
Unfortunately this outputs me the toString() of foo and then the property name of bar. I would like to do the same but without printing anything with the assignation.
(just in case anyone lands here...)
According to this documentation, you can use #code blocks to execute statements without emitting output.
#code{bar=foo}
#{bar.name}

String concatenation in stringtemplate

I'm trying to make a lookup to a hash table m.notes using concatenated value of position.qid and literal ".qid" like this:
$tag(name="itemId", content=m.notes.(position.qid".qid").itemId)$
I tried different options, but I get runtime error. Can someone correct my syntax?
Put the 2 items in an array. StringTemplate concatenates all items in an array (or as they call it, a multi-valued-attribute) when it executes a ToString() on it.
[position.qid, ".qid"]
So, if position.qid evaluates to "hello", this expression would become
hello.qid.
Not sure whether such concatenation is possible in string template. Why don't you use a different method that could do the concatenation and return the value.
e.g:
position.fullQid instead of position.qid
where,
public String getFullQid(){
return getQid() + ".qid";
}
in template group, I can do like this, first, define a concantenate template:
concantenate(substr)::=""
then use as following
(concantenate([position.qid,".qid"]))

what's the meaning of the brackets in Property definition?

What is the meaning of the square brackets around the name of a property in the definition ?
Example :
Public Property [Date] As String
To use reserved keywords as identifiers, the brackets must be used to
distinguish between the identifier and the keyword:
dim [String] As String
public sub [Stop]
end sub
On msdn it says:
Any program element — such as a variable, class, or member — can have
the same name as a restricted keyword. For example, you can create a
variable named Loop. However, to refer to your version of it — which
has the same name as the restricted Loop keyword — you must either
qualify it by preceding it with its full namespace, or enclose it in
square brackets ([ ]), as in the following examples:
Reference here
This syntax allows you to use a reserved word as the name of a member or variable. Not recommended though IMHO from a code maintainability point of view (though see comments below for an alternative point of view on this particular point)!
Particularly not recommended if you're going to declare a property called "Date" as a string, but that's a separate issue...
Date is a reserved keyword in VB.NET, but can be used as a property or variable name if enclosed in square brackets:
http://msdn.microsoft.com/en-us/library/ksh7h19t(v=vs.90).aspx

What does [String] mean in VB.NET?

Does anybody know what does the following construct mean:
Dim s1 as [String]
What do the square brackets mean? And why does the following statement with Integer fail while the one above, with String works?
Dim i1 as [Integer]
Thanks in advance.
THe square brackets is used so that the compiler interprets it as a type, even if it would be a keyword. Imagine for example if you had a class named As:
Dim a As [As]
This is usually only used in auto generated code, so that it works with any type that you throw at it.
The reason that you can't use [Integer] is that Integer is not a data type, it's a keyword. You would have to use the corresponding data type, i.e. [Int32].
Square brackets are used to create a variable that has the same name as a keyword in VB.NET. So they are more often used that way:
Dim [Integer] As Integer
Dim [String] As String
In addition to the other answers:
For the case of using variable names with names the same as types: you shouldn't have to use those in your own code. If you do, you are naming your variable names incredibly poorly, and need to work on using better variable names first!