How to work with velocity context variable as local variable
In velocity i have a problem. Let me explain clearly as below
I have declared in class
private final static int MAX_TOOL_TITLE_LENGTH = 20;
//Putting into context
context.put("maxToolTitleLength","MAX_TOOL_TITLE_LENGTH");
Now in vm file i am setting the context variable value as different
#if(true)
#set($maxToolTitleLength=99)
#end
In the above lines in vm file i set value as 99.i do not want to set this value to context variable.i want to limit the scope of the variable with in the if only. After if i don't want to get the value as 99.it remains same value as in the context. Is it possible in velocity in any of other ways?
Thanks in advance.
java velocity
add a comment |
In velocity i have a problem. Let me explain clearly as below
I have declared in class
private final static int MAX_TOOL_TITLE_LENGTH = 20;
//Putting into context
context.put("maxToolTitleLength","MAX_TOOL_TITLE_LENGTH");
Now in vm file i am setting the context variable value as different
#if(true)
#set($maxToolTitleLength=99)
#end
In the above lines in vm file i set value as 99.i do not want to set this value to context variable.i want to limit the scope of the variable with in the if only. After if i don't want to get the value as 99.it remains same value as in the context. Is it possible in velocity in any of other ways?
Thanks in advance.
java velocity
No, velocity has no scoped blocks.
– RaviH
Feb 14 '14 at 14:09
Then how to get out of problem.if i change the context variable value in template file.it doesn't effect the context variable?
– Laxman
Feb 14 '14 at 14:11
It may be possible to provide a solution if you explain the actual problem you are trying to solve.
– RaviH
Feb 14 '14 at 14:20
IN my project there is a text length validation.i have a four text boxes.for three text boxes i want to allow 20 characters.For other one i required 90 characters to allow.before developers developed that,they take one variable in the context and set value to 20.in velocity template they give the this variable as max length attribute value.if i change the value in template file,it is applying to all text boxes because it is context variable.so how to give a 90 to that particular text box?
– Laxman
Feb 14 '14 at 14:41
add a comment |
In velocity i have a problem. Let me explain clearly as below
I have declared in class
private final static int MAX_TOOL_TITLE_LENGTH = 20;
//Putting into context
context.put("maxToolTitleLength","MAX_TOOL_TITLE_LENGTH");
Now in vm file i am setting the context variable value as different
#if(true)
#set($maxToolTitleLength=99)
#end
In the above lines in vm file i set value as 99.i do not want to set this value to context variable.i want to limit the scope of the variable with in the if only. After if i don't want to get the value as 99.it remains same value as in the context. Is it possible in velocity in any of other ways?
Thanks in advance.
java velocity
In velocity i have a problem. Let me explain clearly as below
I have declared in class
private final static int MAX_TOOL_TITLE_LENGTH = 20;
//Putting into context
context.put("maxToolTitleLength","MAX_TOOL_TITLE_LENGTH");
Now in vm file i am setting the context variable value as different
#if(true)
#set($maxToolTitleLength=99)
#end
In the above lines in vm file i set value as 99.i do not want to set this value to context variable.i want to limit the scope of the variable with in the if only. After if i don't want to get the value as 99.it remains same value as in the context. Is it possible in velocity in any of other ways?
Thanks in advance.
java velocity
java velocity
edited May 2 '14 at 10:38
gerry
6811814
6811814
asked Feb 14 '14 at 14:06
LaxmanLaxman
3110
3110
No, velocity has no scoped blocks.
– RaviH
Feb 14 '14 at 14:09
Then how to get out of problem.if i change the context variable value in template file.it doesn't effect the context variable?
– Laxman
Feb 14 '14 at 14:11
It may be possible to provide a solution if you explain the actual problem you are trying to solve.
– RaviH
Feb 14 '14 at 14:20
IN my project there is a text length validation.i have a four text boxes.for three text boxes i want to allow 20 characters.For other one i required 90 characters to allow.before developers developed that,they take one variable in the context and set value to 20.in velocity template they give the this variable as max length attribute value.if i change the value in template file,it is applying to all text boxes because it is context variable.so how to give a 90 to that particular text box?
– Laxman
Feb 14 '14 at 14:41
add a comment |
No, velocity has no scoped blocks.
– RaviH
Feb 14 '14 at 14:09
Then how to get out of problem.if i change the context variable value in template file.it doesn't effect the context variable?
– Laxman
Feb 14 '14 at 14:11
It may be possible to provide a solution if you explain the actual problem you are trying to solve.
– RaviH
Feb 14 '14 at 14:20
IN my project there is a text length validation.i have a four text boxes.for three text boxes i want to allow 20 characters.For other one i required 90 characters to allow.before developers developed that,they take one variable in the context and set value to 20.in velocity template they give the this variable as max length attribute value.if i change the value in template file,it is applying to all text boxes because it is context variable.so how to give a 90 to that particular text box?
– Laxman
Feb 14 '14 at 14:41
No, velocity has no scoped blocks.
– RaviH
Feb 14 '14 at 14:09
No, velocity has no scoped blocks.
– RaviH
Feb 14 '14 at 14:09
Then how to get out of problem.if i change the context variable value in template file.it doesn't effect the context variable?
– Laxman
Feb 14 '14 at 14:11
Then how to get out of problem.if i change the context variable value in template file.it doesn't effect the context variable?
– Laxman
Feb 14 '14 at 14:11
It may be possible to provide a solution if you explain the actual problem you are trying to solve.
– RaviH
Feb 14 '14 at 14:20
It may be possible to provide a solution if you explain the actual problem you are trying to solve.
– RaviH
Feb 14 '14 at 14:20
IN my project there is a text length validation.i have a four text boxes.for three text boxes i want to allow 20 characters.For other one i required 90 characters to allow.before developers developed that,they take one variable in the context and set value to 20.in velocity template they give the this variable as max length attribute value.if i change the value in template file,it is applying to all text boxes because it is context variable.so how to give a 90 to that particular text box?
– Laxman
Feb 14 '14 at 14:41
IN my project there is a text length validation.i have a four text boxes.for three text boxes i want to allow 20 characters.For other one i required 90 characters to allow.before developers developed that,they take one variable in the context and set value to 20.in velocity template they give the this variable as max length attribute value.if i change the value in template file,it is applying to all text boxes because it is context variable.so how to give a 90 to that particular text box?
– Laxman
Feb 14 '14 at 14:41
add a comment |
3 Answers
3
active
oldest
votes
There is actually a way to introduce local variables in Velocity, though it is a bit surprising:
#foreach ($maxToolTitleLength in [99])
...text that uses this $maxToolTitleLength definition...
#end
## now $maxToolTitleLength is back to whatever value it had before
[99]
is a list with one element, 99
, so this is a loop that executes one time. Within the loop $maxToolTitleLength
has the value 99
. You can also #set ($maxToolTitleLength = $anotherValue)
within the loop. The value from before the #foreach
will be restored after the loop.
add a comment |
The #foreach-hack, as found by @user2624542, works to generate local scopes. However, at least in the quite old Velocity 1.5 of Polarion the behavior inside a macro is different than outside.
Inside macros the scoping works for existing variables (in the global context), but not for undefined ones.
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## undef
#set($foo=0)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
whereas inside a macro
#macro(bar)
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 100 NOT undef
#end
$foo ## undef
#bar()
$foo ## 100 NOT undef
#macro(bar2)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
#end
#set($foo=0)
$foo ## 0
#bar2()
$foo ## 0
add a comment |
It is not possible to change the value of a context object and make this change local only. I don't see why you don't simply use a new variable in your template for this?
#if (true)
#set($thisTitleLength=99)
#end
Or if you do need to use the context variable for this, just restore the context variable after you finish the block where you want to change its value:
#if (true)
#set($maxToolTitleLength=99)
## Do whatever you want with the new value
...
## Now restore its value
#set($maxToolTitleLength=20)
#end
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21781532%2fhow-to-work-with-velocity-context-variable-as-local-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is actually a way to introduce local variables in Velocity, though it is a bit surprising:
#foreach ($maxToolTitleLength in [99])
...text that uses this $maxToolTitleLength definition...
#end
## now $maxToolTitleLength is back to whatever value it had before
[99]
is a list with one element, 99
, so this is a loop that executes one time. Within the loop $maxToolTitleLength
has the value 99
. You can also #set ($maxToolTitleLength = $anotherValue)
within the loop. The value from before the #foreach
will be restored after the loop.
add a comment |
There is actually a way to introduce local variables in Velocity, though it is a bit surprising:
#foreach ($maxToolTitleLength in [99])
...text that uses this $maxToolTitleLength definition...
#end
## now $maxToolTitleLength is back to whatever value it had before
[99]
is a list with one element, 99
, so this is a loop that executes one time. Within the loop $maxToolTitleLength
has the value 99
. You can also #set ($maxToolTitleLength = $anotherValue)
within the loop. The value from before the #foreach
will be restored after the loop.
add a comment |
There is actually a way to introduce local variables in Velocity, though it is a bit surprising:
#foreach ($maxToolTitleLength in [99])
...text that uses this $maxToolTitleLength definition...
#end
## now $maxToolTitleLength is back to whatever value it had before
[99]
is a list with one element, 99
, so this is a loop that executes one time. Within the loop $maxToolTitleLength
has the value 99
. You can also #set ($maxToolTitleLength = $anotherValue)
within the loop. The value from before the #foreach
will be restored after the loop.
There is actually a way to introduce local variables in Velocity, though it is a bit surprising:
#foreach ($maxToolTitleLength in [99])
...text that uses this $maxToolTitleLength definition...
#end
## now $maxToolTitleLength is back to whatever value it had before
[99]
is a list with one element, 99
, so this is a loop that executes one time. Within the loop $maxToolTitleLength
has the value 99
. You can also #set ($maxToolTitleLength = $anotherValue)
within the loop. The value from before the #foreach
will be restored after the loop.
answered Jan 16 '18 at 1:12
user2624542user2624542
312
312
add a comment |
add a comment |
The #foreach-hack, as found by @user2624542, works to generate local scopes. However, at least in the quite old Velocity 1.5 of Polarion the behavior inside a macro is different than outside.
Inside macros the scoping works for existing variables (in the global context), but not for undefined ones.
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## undef
#set($foo=0)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
whereas inside a macro
#macro(bar)
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 100 NOT undef
#end
$foo ## undef
#bar()
$foo ## 100 NOT undef
#macro(bar2)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
#end
#set($foo=0)
$foo ## 0
#bar2()
$foo ## 0
add a comment |
The #foreach-hack, as found by @user2624542, works to generate local scopes. However, at least in the quite old Velocity 1.5 of Polarion the behavior inside a macro is different than outside.
Inside macros the scoping works for existing variables (in the global context), but not for undefined ones.
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## undef
#set($foo=0)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
whereas inside a macro
#macro(bar)
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 100 NOT undef
#end
$foo ## undef
#bar()
$foo ## 100 NOT undef
#macro(bar2)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
#end
#set($foo=0)
$foo ## 0
#bar2()
$foo ## 0
add a comment |
The #foreach-hack, as found by @user2624542, works to generate local scopes. However, at least in the quite old Velocity 1.5 of Polarion the behavior inside a macro is different than outside.
Inside macros the scoping works for existing variables (in the global context), but not for undefined ones.
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## undef
#set($foo=0)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
whereas inside a macro
#macro(bar)
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 100 NOT undef
#end
$foo ## undef
#bar()
$foo ## 100 NOT undef
#macro(bar2)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
#end
#set($foo=0)
$foo ## 0
#bar2()
$foo ## 0
The #foreach-hack, as found by @user2624542, works to generate local scopes. However, at least in the quite old Velocity 1.5 of Polarion the behavior inside a macro is different than outside.
Inside macros the scoping works for existing variables (in the global context), but not for undefined ones.
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## undef
#set($foo=0)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
whereas inside a macro
#macro(bar)
$foo ## undef
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 100 NOT undef
#end
$foo ## undef
#bar()
$foo ## 100 NOT undef
#macro(bar2)
$foo ## 0
#foreach ($foo in [99])
$foo ## 99
#set($foo=100)
$foo ## 100
#end
$foo ## 0
#end
#set($foo=0)
$foo ## 0
#bar2()
$foo ## 0
answered Nov 23 '18 at 11:53
buergibuergi
4,45321212
4,45321212
add a comment |
add a comment |
It is not possible to change the value of a context object and make this change local only. I don't see why you don't simply use a new variable in your template for this?
#if (true)
#set($thisTitleLength=99)
#end
Or if you do need to use the context variable for this, just restore the context variable after you finish the block where you want to change its value:
#if (true)
#set($maxToolTitleLength=99)
## Do whatever you want with the new value
...
## Now restore its value
#set($maxToolTitleLength=20)
#end
add a comment |
It is not possible to change the value of a context object and make this change local only. I don't see why you don't simply use a new variable in your template for this?
#if (true)
#set($thisTitleLength=99)
#end
Or if you do need to use the context variable for this, just restore the context variable after you finish the block where you want to change its value:
#if (true)
#set($maxToolTitleLength=99)
## Do whatever you want with the new value
...
## Now restore its value
#set($maxToolTitleLength=20)
#end
add a comment |
It is not possible to change the value of a context object and make this change local only. I don't see why you don't simply use a new variable in your template for this?
#if (true)
#set($thisTitleLength=99)
#end
Or if you do need to use the context variable for this, just restore the context variable after you finish the block where you want to change its value:
#if (true)
#set($maxToolTitleLength=99)
## Do whatever you want with the new value
...
## Now restore its value
#set($maxToolTitleLength=20)
#end
It is not possible to change the value of a context object and make this change local only. I don't see why you don't simply use a new variable in your template for this?
#if (true)
#set($thisTitleLength=99)
#end
Or if you do need to use the context variable for this, just restore the context variable after you finish the block where you want to change its value:
#if (true)
#set($maxToolTitleLength=99)
## Do whatever you want with the new value
...
## Now restore its value
#set($maxToolTitleLength=20)
#end
answered May 2 '14 at 10:43
gerrygerry
6811814
6811814
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21781532%2fhow-to-work-with-velocity-context-variable-as-local-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
No, velocity has no scoped blocks.
– RaviH
Feb 14 '14 at 14:09
Then how to get out of problem.if i change the context variable value in template file.it doesn't effect the context variable?
– Laxman
Feb 14 '14 at 14:11
It may be possible to provide a solution if you explain the actual problem you are trying to solve.
– RaviH
Feb 14 '14 at 14:20
IN my project there is a text length validation.i have a four text boxes.for three text boxes i want to allow 20 characters.For other one i required 90 characters to allow.before developers developed that,they take one variable in the context and set value to 20.in velocity template they give the this variable as max length attribute value.if i change the value in template file,it is applying to all text boxes because it is context variable.so how to give a 90 to that particular text box?
– Laxman
Feb 14 '14 at 14:41