速度模板循环遍历数组以创建字符串
velocity template loop through array to create string
我正在尝试使用 AWS appsync resolver
中的 velocity templating language
通过遍历字符数组来创建字符串。
给定数组 listOfWords = ["好" "克力"]
我将如何实现 queryString = "+\"好\" +\"克力\""
的字符串输出
到目前为止,我已经完成了这样的事情:
24: #set($listOfWords = ["好" "克力"])
25: #set($queryString = "")
26: #foreach($word in $listOfWords)
27: #if( $velocityCount == 1 )
28: #set($queryString = "+\"$word\"")
29: #else
30: #set($queryString = $queryString +"+\"$word\"")
31: #end
32: #end
这个returns错误:
Encountered \"$word\" at velocity[line 28, column 37]\nWas expecting one of:\n <RPAREN> ...\n <WHITESPACE> ...\n \"-\" ...\n \"+\" ...\n \"*\" ...\n \"/\" ...\n \"%\" ...\n <LOGICAL_AND> ...\n <LOGICAL_OR> ...\n <LOGICAL_LT> ...\n <LOGICAL_LE> ...\n <LOGICAL_GT> ...\n <LOGICAL_GE> ...\n <LOGICAL_EQUALS> ...\n <LOGICAL_NOT_EQUALS> ...\n
我也试过了
#foreach( $word in $listOfWords )
#if( $velocityCount == 1 )
#set($queryString = "+" + "\" + "\"" + $word + "\" + "\"") line 27
#else
#set($queryString = $queryString + "+" + "\" + "\"" + $word + "\" + "\"")
#end
#end
)
但似乎导致了词法错误:
"Lexical error, Encountered: \"\\"\" (34), after : \"\\\\\" at *unset*[line 27, column 64]"
无需构建 VTL 变量,您可以直接构建字符串作为输出。有点像 VTL 文档中的这个例子;
<ul>
#foreach( $product in $allProducts )
<li>$product</li>
#end
</ul>
你可以做到:
#set($listOfWords = ["好" "克力"])
#set($q = '"')
#set($queryString = "")
#foreach($word in $listOfWords)
#if( $velocityCount == 1 )
#set($queryString = "$q$word$q")
#else
#set($queryString = "$queryString+$q$word$q")
#end
#end
我正在尝试使用 AWS appsync resolver
中的 velocity templating language
通过遍历字符数组来创建字符串。
给定数组 listOfWords = ["好" "克力"]
我将如何实现 queryString = "+\"好\" +\"克力\""
到目前为止,我已经完成了这样的事情:
24: #set($listOfWords = ["好" "克力"])
25: #set($queryString = "")
26: #foreach($word in $listOfWords)
27: #if( $velocityCount == 1 )
28: #set($queryString = "+\"$word\"")
29: #else
30: #set($queryString = $queryString +"+\"$word\"")
31: #end
32: #end
这个returns错误:
Encountered \"$word\" at velocity[line 28, column 37]\nWas expecting one of:\n <RPAREN> ...\n <WHITESPACE> ...\n \"-\" ...\n \"+\" ...\n \"*\" ...\n \"/\" ...\n \"%\" ...\n <LOGICAL_AND> ...\n <LOGICAL_OR> ...\n <LOGICAL_LT> ...\n <LOGICAL_LE> ...\n <LOGICAL_GT> ...\n <LOGICAL_GE> ...\n <LOGICAL_EQUALS> ...\n <LOGICAL_NOT_EQUALS> ...\n
我也试过了
#foreach( $word in $listOfWords )
#if( $velocityCount == 1 )
#set($queryString = "+" + "\" + "\"" + $word + "\" + "\"") line 27
#else
#set($queryString = $queryString + "+" + "\" + "\"" + $word + "\" + "\"")
#end
#end
)
但似乎导致了词法错误:
"Lexical error, Encountered: \"\\"\" (34), after : \"\\\\\" at *unset*[line 27, column 64]"
无需构建 VTL 变量,您可以直接构建字符串作为输出。有点像 VTL 文档中的这个例子;
<ul>
#foreach( $product in $allProducts )
<li>$product</li>
#end
</ul>
你可以做到:
#set($listOfWords = ["好" "克力"])
#set($q = '"')
#set($queryString = "")
#foreach($word in $listOfWords)
#if( $velocityCount == 1 )
#set($queryString = "$q$word$q")
#else
#set($queryString = "$queryString+$q$word$q")
#end
#end