在循环内访问变量,在Twig中访问变量

时间:2018-02-12 18:10:42

标签: twig

我想做以下事情:

def foo(x1, x2, *args):
    varargin = args
    nargin = 2 + len(varargin) # 2 mandatory arguments (x1 and x2) 
    # ...

是否可以在变量{% for i in 0..10 %} {% if content_{{ i }}_raw == 2 %} ... {% endif %} {% endfor %} 中获取{{ i }}并将content_1_raw替换为1的值?

1 个答案:

答案 0 :(得分:2)

是。 j = 0 For Each varField In FieldList j_UBound = j_UBound + 1 Next varField ReDim arrTemp2(j_LBound To j_UBound) For Each varField In FieldList FetchArray(j, i_UBound) = CStr(varField) j = j + 1 Next varField 变量保存当前上下文中的所有变量。您可以使用括号表示法或使用attribute function

来访问其值
i_LBound = LBound(FetchArray, 2)
i_UBound = UBound(FetchArray, 2)

j_LBound = LBound(FetchArray, 1)
j_UBound = UBound(FetchArray, 1)

If UBound(arrTemp1) <> i_UBound + 1 Then
    ReDim arrTemp1(i_LBound To i_UBound + 1)
    arrTemp1(i_UBound + 1) = vbNullString   ' The 'Join' operation will insert a trailing row
End If                                      ' delimiter here (Not required by the last chunk)

If UBound(arrTemp2) <> j_UBound Then
    ReDim arrTemp2(j_LBound To j_UBound)
End If


' Data body. This is heavily optimised to avoid VBA String functions with allocations

For i = i_LBound To i_UBound Step 1

    ' If this is confusing... Were you expecting FetchArray(i,j)? i for row, j for column?
    ' FetchArray comes from RecordSet.GetRows(), which returns a TRANSPOSED array: i and j
    ' are still the field and record ordinals, row(i) and column(j) in the output file.

    For j = j_LBound To j_UBound

        If IsNull(FetchArray(j, i)) Then
            arrTemp2(j) = ""
        Else
            arrTemp2(j) = FetchArray(j, i)  ' confused? see he note above
        End If

        If CleanupText Or (i_UBound = 0) Then  ' (i_UBound=0): always clean up field names

            arrBytes = arrTemp2(j) ' Integer arithmetic is faster than string-handling for
                                   ' this: all VBA string operations require an allocation

            For k = LBound(arrBytes) To UBound(arrBytes) Step 2

                Select Case arrBytes(k)
                Case 10, 13, 9, 160
                    If arrBytes(k + 1) = 0 Then
                        arrBytes(k) = 32 ' replaces CR, LF, Tab, and non-breaking
                    End If               ' spaces with the standard ANSI space
                Case 44
                    If Not CoerceText Then
                        If arrBytes(k + 1) = 0 Then
                            arrBytes(k) = 32 ' replace comma with the ANSI space
                        End If
                    End If
                Case 34
                    If arrBytes(k + 1) = 0 Then
                        arrBytes(k) = 39  ' replaces double-quote with single quote
                    End If
                End Select

            Next k

            arrTemp2(j) = arrTemp2(j)

        End If  ' cleanup


        If CoerceText Then  ' encapsulate all fields in quotes, numeric or not

           arrTemp3(1) = arrTemp2(j)
           arrTemp2(j) = Join$(arrTemp3, vbNullString)

        ElseIf (i = 0) And (i = i_UBound) Then ' always encapsulate field names

           arrTemp3(1) = arrTemp2(j)
           arrTemp2(j) = Join$(arrTemp3, vbNullString)

        Else ' selective encapsulation, leaving numeric fields unencapsulated:
             ' we *could* do this by reading the ADODB field types: but that's
             ' slower, and you may be 'caught out' by provider-specific types.

            arrBytes = arrTemp2(j)

            boolNumeric = True

            For k = LBound(arrBytes) To UBound(arrBytes) Step 2
                If arrBytes(k) < 43 Or arrBytes(k) > 57 Then

                    If arrBytes(k) <> 69 Then
                        boolNumeric = False
                        Exit For
                    Else
                        If k > UBound(arrBytes) - 5 Then
                            boolNumeric = False
                            Exit For
                        ElseIf arrBytes(k + 2) = 45 Then
                            ' detect "1.234E-05"
                        ElseIf arrBytes(k + 2) = 43 Then
                            ' detect "1.234E+05"
                        Else
                            boolNumeric = False
                            Exit For
                        End If
                    End If

                End If
            Next k

            If boolNumeric Then
               For k = 1 + LBound(arrBytes) To UBound(arrBytes) Step 2
                   If arrBytes(k) <> 0 Then
                       boolNumeric = False
                       Exit For
                   End If
               Next k
            End If

           arrBytes = vbNullString

           If Not boolNumeric Then ' text field, encapsulate it
               arrTemp3(1) = arrTemp2(j)
               arrTemp2(j) = Join(arrTemp3, vbNullString)
           End If

        End If ' CoerceText

    Next j

   arrTemp1(i) = Join(arrTemp2, COMMA)

Next i

iRowCount = iRowCount + i - 2


'   **** WHY WE 'PUT' A BYTE ARRAY INSTEAD OF A VBA STRING VARIABLE  **** ****
'
'       Put #hndFile, , StrConv(Join(arrTemp1, EOROW), vbUnicode)
'       Put #hndFile, , Join(arrTemp1, EOROW)
'
'   If you pass unicode, Wide or UTF-16 string variables to PUT, it prepends a
'   Unicode Byte Order Mark to the data which, when written to your file, will
'   render the field names illegible to Microsoft's JET ODBC and ACE-OLEDB SQL
'   drivers (which can actually read unicode field names, if the helpful label
'   isn't in the way). The primeval 'PUT' statement writes a Byte array as-is.
'
'   **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****


arrBytes = Join$(arrTemp1, vbCrLf)


If hndFile = 0 Then

    i_Offset = 1
    If Len(Dir(OutputFile)) > 0 Then
        VBA.FileSystem.Kill OutputFile
    End If

    WaitForFileDeletion OutputFile

    hndFile = FreeFile
    Open OutputFile For Binary Access Write As #hndFile

End If


Put #hndFile, i_Offset, arrBytes
i_Offset = i_Offset + 1 + UBound(arrBytes)
Erase arrBytes


If rst.EOF Then
    Erase FetchArray
    FetchArray = Empty
Else
    If IsMissing(FieldList) Then
        FetchArray = rst.GetRows(FETCH_ROWS)
    Else
        FetchArray = rst.GetRows(FETCH_ROWS, , FieldList)
    End If
End If

我在这里写了更多相关细节:Symfony2 - How to access dynamic variable names in twig

此外,您还可以使用string concatenation operator代替On Error Resume Next If hndFile <> 0 Then Close #hndFile End If Erase arrBytes Erase arrTemp1 Erase arrTemp2 Exit Function (代字号,Resume ExitSub string interpolation):

End Function