使用Google闭包模板时,如何在Soy文件中迭代对象?

Cur*_*tor 14 javascript google-closure

我想创建自己的模板,我可以传递一个对象,并让Soy模板迭代对象并拉出键和值.

如果我在JavaScript中有对象并调用Soy模板:

var obj = {'one':'a', 'two':b, 'three':c};
nameSpace.templateName({'paramValue': obj});
Run Code Online (Sandbox Code Playgroud)

我如何获得['one', 'two', 'three']价值?通常我会使用jQuery的each()功能,但我不知道如何在没有将对象转换为数组的情况下在Soy文件中做类似的事情.

我正在使用的对象具有已知的形式(没有嵌套对象,或者如果有的话,它们是提前知道并且已知的深度).欢迎使用嵌套对象的此答案或一般对象案例的答案.

{namespace nameSpace}

/**
 * Prints keys and values of the object
 * @param paramValue object with keys and values
 */
{template .templateName}
    {$paramValue[0]}    // undefined
    {$paramValue.Keys}  // undefined
    {$paramValue.keys}  // undefined
    {$paramValue.one}   // prints 'a'
    {foreach $val in $paramValue}
      // never reached
    {/foreach} 
{/template}
Run Code Online (Sandbox Code Playgroud)

ale*_*lex 23

您现在可以使用该keys()功能获取它们.

{foreach $key in keys($paramValue)}
  key:   {$key}
  value: {$paramValue[$key]}
{/foreach} 
Run Code Online (Sandbox Code Playgroud)