Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays JsRender-动态数组,辅助函数_Arrays_Json_Jsrender - Fatal编程技术网

Arrays JsRender-动态数组,辅助函数

Arrays JsRender-动态数组,辅助函数,arrays,json,jsrender,Arrays,Json,Jsrender,我有一个带有JsRender和JSON文件的网站。 我的JSRender代码: {{for ~getModel(cards)}} {{:id}} {{/for}} 我的JSON文件: { "alpha": { "cards": [{ "id": "alpha-01" }, { "id": "alpha-02" }, { "id": "alpha-03"

我有一个带有JsRender和JSON文件的网站。
我的JSRender代码:

{{for ~getModel(cards)}}
{{:id}}
{{/for}}
我的JSON文件:

{
    "alpha": {
        "cards": [{
            "id": "alpha-01"
        }, {
            "id": "alpha-02"
        }, {
            "id": "alpha-03"
        }]
    },
    "beta": {
        "cards": [{
            "id": "beta-01"
        }, {
            "id": "beta-02"
        }]
    }
}
{{for~getModel(cards)}}
中是
cards
,后缀。 我的JsRender助手连接前缀和后缀,结果是一个字符串。 这个字符串应该是
for
循环的数组。后缀是一个动态部分,是URL中的一个参数(
?model=alpha

for循环应该通过数组
alpha.cards
->
{{for~getModel(alpha.cards)}}

但是当我连接动态部分和后缀时,返回值是一个字符串,而
for
循环不起作用。似乎助手的值(return)必须是一个数组


这是对的还是有其他解决方案?

是-您需要返回一个数组。JsRender针对javascript对象和数组(通常是对象和数组的层次结构)进行渲染。如果您的字符串是JSON字符串,则需要先转换/eval以生成相应的对象或数组…

谢谢您的回复。我的解决方案是:
$.views.helpers({getModel:function(){returnthis.data[byModel];}})并且它工作
byModel
包含
alpha
作为字符串。