How to work with velocity context variable as local variable












2















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.










share|improve this question

























  • 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


















2















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.










share|improve this question

























  • 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
















2












2








2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





















  • 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














3 Answers
3






active

oldest

votes


















3














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.






share|improve this answer































    1














    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





    share|improve this answer































      0














      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





      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        3














        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.






        share|improve this answer




























          3














          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.






          share|improve this answer


























            3












            3








            3







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 16 '18 at 1:12









            user2624542user2624542

            312




            312

























                1














                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





                share|improve this answer




























                  1














                  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





                  share|improve this answer


























                    1












                    1








                    1







                    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





                    share|improve this answer













                    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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 11:53









                    buergibuergi

                    4,45321212




                    4,45321212























                        0














                        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





                        share|improve this answer




























                          0














                          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





                          share|improve this answer


























                            0












                            0








                            0







                            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





                            share|improve this answer













                            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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 2 '14 at 10:43









                            gerrygerry

                            6811814




                            6811814






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Berounka

                                Sphinx de Gizeh

                                Different font size/position of beamer's navigation symbols template's content depending on regular/plain...