How to assign a variable in MVEL Template - mvel

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}

Related

NLog Conditional Variable Value

In NLog, is there a way to have a variable with a conditional value? I've seen this: https://github.com/NLog/NLog/wiki/When-Layout-Renderer and tried the following:
<variable name="EnvironmentString" value="${when:when='${IsProd}' == 'true':Prod:else:Stage}"/>
but the value is just returned as a literal; the logic is not being processed.
Thanks,
1) Change the syntax to this:
${when:when='${var:IsProd}'=='true':inner=Prod:else=Stage}
Note:
:else= rather than :else:
:inner=
var:IsProd (assuming IsProd is another variable)
2) Move the whole conditional to the final Layout
<target ... layout="other stuff|${when:when='${var:IsProd}'=='true':inner=Prod:else=Stage}|other stuff" ... />
I've not managed to get conditionals working in variables. Perhaps someone else could say why.
This should work.
But it depends how the variable is used.
If you use:
${EnvironmentString}
Then it's evaluated when loading the configuration, and so you could use it for all parameters.
To evaluate it dynamically, use
${var:EnvironmentString}
But please note that ${var} only works if the parameter of the Target/Layout is of type Layout

String template to set the default value of PARAMETER

Is it possible, in ABAP, to evaluate string templates dynamically?
Normally, you will have some string template in code that will be checked by the compiler. (The variables in the curly brackets are checked by the compiler at compile time).
However, is it possible to have a string evaluated at runtime?
So, instead of:
data(val) = |System ID: { sy-sysid }|.
I would like the string to be interpolated to come from elsewhere, for example:
parameter: p_file type string lower case default '/mnt/{ sy-sysid }/file.txt'.
In this case, I would like to have the value of p_file to be evaluated at runtime to substitute the variable (sy-sysid) with the runtime value.
You could, of course, program your own substitution by finding all occurrences of variables with curly brackets with a regex expression, then evaluate the variable values with ASSIGN and substitute them back into the string, but I am looking for a built-in way to do this.
Sorry, this is maybe a stupid example, but hopefully you understand what I mean. (If not, please let me know in the comments and I will try and clarify).
The problem in your snippet is not with string template but with PARAMETER behavior. It does not allow dynamics in DEFAULT clause.
To achieve what you want you should use INITIALIZATION and set path value in runtime:
parameter: p_file type string lower case.
INITIALIZATION.
p_file = | /mnt/{ sy-sysid }/file.txt |.
Unfortunately, the example you gave, does not make any sense to me. ABAP String templates are evaluated at run-time and type-checked at compile-time.
In your example, it is always the run-time value of SY-SYSID that will be written to the variable.
I guess what you want to do is circumvent compile-time checks for expressions inside a string template.
Please try to give us your actual use case, so maybe we find an even better solution to your problem.
However, here is what I think could help you:
Personally, I do not recommend to write code like the one below, because it is extremely error-prone likely to mislead other programmers and because there is very likely a better solution.
Given that you know the name of a variable at run-time, try this:
".. say LV_VARNAME is a charlike variable that contains a
" variable name at runtime.
"NOTE that the variable LV_VARNAME must be visible in the scope of the
"following code.
FIELD-SYMBOLS: <my_var> TYPE any.
ASSIGN (lv_varname) TO <my_var>.
DATA(lv_str) = |The value is { <my_var> }|.

How to apply more than one function to a passed in live template variable?

I'm trying to build a Python Unit Test File Template in PyCharm. The overall result I want to achieve is:
A user creates a new file with my template, say "widget_builder.py"
Inside the template I want to create the class name by taking the file name "widget_builder" and turning it into "WidgetBuilderTests"
It looks like I need to use a Live Template to manipulate the file template variable $FILE_NAME$?
How can I create a Live Template that given a passed in variable (in this case $FILE_NAME$), applies both the underscoresToCamelCase and capitalize functions to it?
If I declare the Template text as:
$CLASS_NAME$
...and then edit variables, how can I reference a passed in variable of '$FILE_NAME$'?
I'd imagine it to look something like this, but I just can't get it to work:
I'm sure there must be a way to do this, but I just can't quite wrap my head round it.
Is this possible? Thanks!
EDIT
I've got a bit further. If I define the template as this:
If I then use it, this happens:
So the end result of $CLASS_NAME$ (WidgetBuilder) on the left is what I want, but I don't want $FILE_NAME$ (widget_builder) to be there once I hit return.
So your problem here is that $FILE_NAME$ is not a native variable in the live templates, merely an arbitrary name. What you actually want to be using is another function: fileNameWithoutExtension().
So your template would look something like:

Why is variable substitution only recommended for values that wont change during lifetime of a widget?

Regarding Variable Substitution in Dojo
Variable Substitution:
A template can have values set on DOM rendering
though the use of a simple variable placeholder syntax, which looks
like this:
${property}
According to the documentation
Variable substitution in a template is only recommended for values
that will not be changed during the lifetime of the widget. In other
words, if you expect to be able to set the value of a property in a
widget during the lifetime of your application programmatically, we
recommend instead using your widget's postCreate method to set any
variables programmatically through your widget's set() method.
Can someone explain why this recommendation is made?
Variable substitution in Dojo has no binding.
This mean it wont change even if you change the actual value of the variable.
If a binding is needed, then you can use an attach point and a setter for that value. It will then have a binding and the UI will be updated with the new value.
Something like this:
_setLabelAttr : {
node : "_tplLabelNode",
type : "innerHTML"
},
Will bind the innerHTML of attach point _tplLabelNode to the "label" property of the widget.
So widget.set('label', 'foo'); will update the UI.
However <div>${label}</div> has no bind. ${label} will be replaced at creation of the widget and will never be updated

In Emacs how do I make a local variable safe to be set in a file for all possible values

In Elisp I have introduced for a special custom mode a variable like:
(defvar leo-special-var "")
(make-variable-buffer-local 'leo-special-var)
Now I set this variable in files I with the lines (in the file to edit):
# Local Variables:
# leo-special-var: "-d http://www.google.com.au"
# End:
And I want to consider this variable as "safe for all its values. That's why safe-local-variable-values doesn't help. Instead I tried (in the lisp code):
# setting the symbol property of the variable
(put 'leo-special-var 'safe-local-variable 'booleanp)
but without success. Do I do something wrong when setting the symbol property? Or is there another way?
You want to use
(put 'leo-special-var 'safe-local-variable #'stringp)
to say that it is safe as long as it's a string.
If you really want to state that it is safe for all values then use this:
(put 'leo-special-var 'safe-local-variable (lambda (_) t))
The function to test safety here returns non-nil for any value.
(But I'd think that you probably do not want to state that a variable is safe for any value.)
It's in the manual: (elisp) File Local Variables
You can specify safe values for a variable with a
`safe-local-variable' property. The property has to be a function of
one argument; any value is safe if the function returns non-`nil' given
that value. Many commonly-encountered file variables have
`safe-local-variable' properties; these include `fill-column',
`fill-prefix', and `indent-tabs-mode'. For boolean-valued variables
that are safe, use `booleanp' as the property value. Lambda
expressions should be quoted so that `describe-variable' can display
the predicate.
When defining a user option using `defcustom', you can set its
`safe-local-variable' property by adding the arguments `:safe FUNCTION'
to `defcustom' (*note Variable Definitions::).