应用一个把手助手到另一个把手助手

huangapple go评论62阅读模式
英文:

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}} {{&quot;hostname.keyword&quot;}} {{/each}})}}</div>

导致以下错误:

在第2行解析错误:
.../h5&gt;&lt;div&gt;{{unique ({{#each data}} {{&quot;ho
----------------------^
期望 'ID'、'STRING'、'NUMBER'、'BOOLEAN'、'UNDEFINED'、'NULL'、'DATA',得到 'OPEN_BLOCK'

以下不会产生任何错误,但不会返回任何内容:

<div>{{unique data &quot;hostname.keyword&quot;}}</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}} {{&quot;hostname.keyword&quot;}} {{/each}}

This contains duplicate values and so I defined a helper to remove those duplicates.

handlebars.registerHelper(&quot;unique&quot;, (context) =&gt;
  [...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,

&lt;div&gt;{{unique ({{#each data}} {{&quot;hostname.keyword&quot;}} {{/each}})}}&lt;/div&gt;

results in the following error:

    Parse error on line 2:
.../h5&gt;&lt;div&gt;{{unique ({{#each data}} {{&quot;ho
----------------------^
Expecting &#39;ID&#39;, &#39;STRING&#39;, &#39;NUMBER&#39;, &#39;BOOLEAN&#39;, &#39;UNDEFINED&#39;, &#39;NULL&#39;, &#39;DATA&#39;, got &#39;OPEN_BLOCK&#39;

The following does not produce any error, but it returns nothing:

&lt;div&gt;{{unique data &quot;hostname.keyword&quot;}}&lt;/div&gt;

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(&#39;&lt;div&gt;{{#each (unique data &quot;hostname.keyword&quot;)}}{{this}}{{/each}}&lt;/div&gt;&#39;);

Handlebars.registerHelper(&quot;unique&quot;, (context, key) =&gt; {
  return [...new Set(context.map(item =&gt; item[key]))];
});

const data = [
  {
    &quot;hostname.keyword&quot;: &quot;a&quot;
  },
  {
    &quot;hostname.keyword&quot;: &quot;b&quot;
  },
  {
    &quot;hostname.keyword&quot;: &quot;a&quot;
  }
];

const output = template({ data });

document.body.innerHTML = output;
console.log(output);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 06:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定