速度模板遍历数组以创建字符串 [英] velocity template loop through array to create string
问题描述
我试图在AWS appsync resolver
中使用velocity templating language
通过循环字符数组来创建字符串.
I'm trying to use the velocity templating language
in an AWS appsync resolver
to create a string by looping through an array of characters.
给出数组listOfWords = ["好" "克力"]
,我将如何实现queryString = "+\"好\" +\"克力\""
Given the array listOfWords = ["好" "克力"]
how would I achieve the string output of queryString = "+\"好\" +\"克力\""
到目前为止,我已经完成了类似的工作:
So far I have managed something like this:
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
这将返回错误:
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
)
但是似乎导致了词法错误:
But seem to be causing a lexical error:
"Lexical error, Encountered: \"\\\"\" (34), after : \"\\\\\\\\\" at *unset*[line 27, column 64]"
推荐答案
您可以这样做:
#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
这篇关于速度模板遍历数组以创建字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!