Uploaded image for project: 'Apache Freemarker'
  1. Apache Freemarker
  2. FREEMARKER-70

Loop Variables Not Resolving when used in string interpolations

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.26-incubating
    • Fix Version/s: 2.3.27-incubating
    • Component/s: engine
    • Labels:
      None

      Description

      When invoking a macro inside of a #list the loop variable is not in scope to be used resolving the argument values to the macro call.

      Example

      
      <#macro button caption>
      content irrelevant.
      </#macro>
      
      
      <#list ["a","b","c"]>
      <#items as loopvar>
      ${loopvar?counter} -- works
      ${loopvar?index} -- works
      <p class="${loopvar?index}"> -- works
      <@button caption="${loopvar?index}" /> -- FAILS
      </#items>
      </#list>
      
      

      The @button macro call fails with the message:
      The left hand operand of ?index must be a loop variable, but there's no loop variable in scope with this name: loopvar

      Shouldn't loopvar be in scope for evaluating those macro argument values?

        Activity

        Hide
        ddekany Daniel Dekany added a comment - - edited

        The problem is not with macros, but with interpolations inside strings, which you happen to use in that macro call. But something like this on the top-level will fail too:

        <#list 1..2 as loopvar>${"${loopvar?index}"}</#list>
        

        while if you call the macro like this, it will work:

        <@button caption=loopvar?index?string />
        

        So the bug that will have to be fixed here is that because the string itself is a like a small template that's parsed separately, it doesn't inherit the loop variable stack from the enclosing parser.

        Show
        ddekany Daniel Dekany added a comment - - edited The problem is not with macros, but with interpolations inside strings, which you happen to use in that macro call. But something like this on the top-level will fail too: <#list 1..2 as loopvar>${ "${loopvar?index}" }</#list> while if you call the macro like this, it will work: <@button caption=loopvar?index?string /> So the bug that will have to be fixed here is that because the string itself is a like a small template that's parsed separately, it doesn't inherit the loop variable stack from the enclosing parser.
        Hide
        ddekany Daniel Dekany added a comment -

        Fix committed.

        Show
        ddekany Daniel Dekany added a comment - Fix committed.

          People

          • Assignee:
            Unassigned
            Reporter:
            bill_parkinson@yahoo.com Bill Parkinson
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development