英文:
Applying a handlebar helper to another handlebar
问题
以下是翻译好的部分:
我正在尝试使用Handlebars来返回列中的所有不同值。
以下是Handlebars模板,返回了数据框中“hostname”列的所有值。
{{#each data}} {{"hostname.keyword"}} {{/each}}
这包含重复的值,因此我定义了一个帮助函数来去重。
handlebars.registerHelper("unique", (context) =>
[...new Set(context)]);
我无法弄清楚如何将Handlebars #each
块返回的结果作为参数传递给帮助函数。例如,
<div>{{unique ({{#each data}} {{"hostname.keyword"}} {{/each}})}}</div>
导致以下错误:
在第2行解析错误:
.../h5><div>{{unique ({{#each data}} {{"ho
----------------------^
期望 'ID'、'STRING'、'NUMBER'、'BOOLEAN'、'UNDEFINED'、'NULL'、'DATA',得到 'OPEN_BLOCK'
以下不会产生任何错误,但不会返回任何内容:
<div>{{unique data "hostname.keyword"}}</div>
这是否可能?如果可能,正确的语法是什么?
英文:
I am trying to use Handlebars to return all distinct values in a column.
The following Handlebars template returns all the values in the column "hostname" of my dataframe.
{{#each data}} {{"hostname.keyword"}} {{/each}}
This contains duplicate values and so I defined a helper to remove those duplicates.
handlebars.registerHelper("unique", (context) =>
[...new Set(context)]);
I'm not able to figure out how to pass the result returned by the Handlebars #each
block as an argument to the helper. For instance,
<div>{{unique ({{#each data}} {{"hostname.keyword"}} {{/each}})}}</div>
results in the following error:
Parse error on line 2:
.../h5><div>{{unique ({{#each data}} {{"ho
----------------------^
Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'OPEN_BLOCK'
The following does not produce any error, but it returns nothing:
<div>{{unique data "hostname.keyword"}}</div>
Is it possible to do this? And if so, what's the correct syntax?
答案1
得分: 1
我不认为有一种方法可以将块助手(例如 #each
)的输出通过内联助手(例如 unique
)进行处理。相反,您需要自定义助手执行两个操作:将对象映射到指定键(hostname.keyword
)的值,然后过滤结果集以仅包含唯一值。一旦您获得了映射和过滤后的结果,您可以将其传递给 #each
块助手。
const template = Handlebars.compile('<div>{{#each (unique data "hostname.keyword")}}{{this}}{{/each}}</div>');
Handlebars.registerHelper("unique", (context, key) => {
return [...new Set(context.map(item => item[key]))];
});
const data = [
{
"hostname.keyword": "a"
},
{
"hostname.keyword": "b"
},
{
"hostname.keyword": "a"
}
];
const output = template({ data });
document.body.innerHTML = output;
console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>
英文:
I don't think there is a way to take the output of a block helper - like #each
- and process it through an inline helper, like unique
. What you would have to do instead is to have your custom helper do both the mapping of the objects to their value at the specified key (hostname.keyword
) and then also filter the result set so as to have only unique values. Once you have the mapped and filtered result, you would pass that to the #each
block helper.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const template = Handlebars.compile('<div>{{#each (unique data "hostname.keyword")}}{{this}}{{/each}}</div>');
Handlebars.registerHelper("unique", (context, key) => {
return [...new Set(context.map(item => item[key]))];
});
const data = [
{
"hostname.keyword": "a"
},
{
"hostname.keyword": "b"
},
{
"hostname.keyword": "a"
}
];
const output = template({ data });
document.body.innerHTML = output;
console.log(output);
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论