Velocity
  1. Velocity
  2. VELOCITY-808

Proposal to add ability to pass variables to #parse()

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.7
    • Fix Version/s: None
    • Component/s: Engine
    • Labels:

      Description

      I think it would be very useful to be able to pass variables to #parse(), just like to #macro():

      #parse("tpl.vm" $var1 $var2 $var3)

      This would add $var's to a local scope of "tpl.vm".

      Current approach of adding vars to a parent template above pollutes global namespace.

        Activity

        Hide
        Jarkko Viinamäki added a comment -

        -1. I don't see an important real life use case for this and it would increase complexity.

        Show
        Jarkko Viinamäki added a comment - -1. I don't see an important real life use case for this and it would increase complexity.
        Hide
        Nathan Bubna added a comment -

        hmm. that is better, but the $args variable would be global and get stomped in recursive or multi-level #parses, which has the potential to cause many headaches. i would suggest that the $args would need to become $template.args (i.e. put into the $template scope object) so that it is properly nested/popped/etc. you should probably even override the template.provide.scope.control=false default and automatically turn $template on when #parse is invoked with arguments, to ensure that the template has access to $template.args.

        or, if you want named arguments, you could have #parse only accept a map (e.g. #parse('tpl.vm'

        {'foo':$bar}

        ) ) and then automatically copy those map values to the $template scope object's storage to make $template.foo automatically available.

        Show
        Nathan Bubna added a comment - hmm. that is better, but the $args variable would be global and get stomped in recursive or multi-level #parses, which has the potential to cause many headaches. i would suggest that the $args would need to become $template.args (i.e. put into the $template scope object) so that it is properly nested/popped/etc. you should probably even override the template.provide.scope.control=false default and automatically turn $template on when #parse is invoked with arguments, to ensure that the template has access to $template.args. or, if you want named arguments, you could have #parse only accept a map (e.g. #parse('tpl.vm' {'foo':$bar} ) ) and then automatically copy those map values to the $template scope object's storage to make $template.foo automatically available.
        Hide
        Sergiy Kovalchuk added a comment -

        Actually after thinking about it I realized that those "arguments" don't have any sense if they are not mapped to anything. So maybe it would be possible to map them to something like $args[]. For example (using commas to separate arguments looks more natural):

        #set($var2 = 2)
        #set($var3 = 3)
        #parse("tpl.vm", 10, $var2+$var3)

        With tpl.vm being:

        argument1= $

        {args[0]}

        //10, comes from argument
        argument2 = $

        {args[1]}

        //5, from argument
        var3 = $

        {var3}

        //3, comes from global scope

        Show
        Sergiy Kovalchuk added a comment - Actually after thinking about it I realized that those "arguments" don't have any sense if they are not mapped to anything. So maybe it would be possible to map them to something like $args[]. For example (using commas to separate arguments looks more natural): #set($var2 = 2) #set($var3 = 3) #parse("tpl.vm", 10, $var2+$var3) With tpl.vm being: argument1= $ {args[0]} //10, comes from argument argument2 = $ {args[1]} //5, from argument var3 = $ {var3} //3, comes from global scope
        Sergiy Kovalchuk created issue -

          People

          • Assignee:
            Unassigned
            Reporter:
            Sergiy Kovalchuk
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:

              Development