移除Handlebarjs中ul内部的额外li。

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

Remove extra li inside of ul in handlebarjs

问题

我有一个handlebar的HTML

<ul><li>{{#listConstruct product}}{{this}}{{/listConstruct}}</li></ul>

需要输出像这样

<ul>
<li>Pen</li>
<li>Pencil</li>
<li>Notebook</li>
</ul>

我写了一个名为listConstruct的辅助函数,如下所示。

Handlebars.registerHelper('listConstruct', function(items) {
   let res = '';
    items.forEach((val)=>{res+= '<li>'+val+'</li>'});
   return res;
 });

但结果输出为:

<ul><li><li>Pen</li><li>Pencil</li><li>Notebook</li></li></ul>

如何移除UL内部的额外LI。

英文:

I have a handlebar html

<ul><li>{{#listConstruct product}}{{this}}{{/listConstruct}}</li></ul>

Need to have output like this

<ul>
<li>Pen</li>
<li>Pencil</li>
<li>Notebook</li>
</ul>

I wrote a listConstruct helper like this.

Handlebars.registerHelper('listConstruct', function(items) {
   let res = '';
    items.forEach((val)=>{res+= '<li>'+val+'</li>'});
   return res;
 });

But this result in output like this

<ul><li><li>Pen</li><li>Pencil</li><li>Notebook</li></li></ul>

How to remove the extra li inside of UL.

答案1

得分: 0

I truly do not understand why you cannot use a simple #each as in:

<ul>
  {{#each items}}
    <li>{{this}}</li>
  {{/each}}
</ul>

If you absolutely must add your template within the <li>, then you can do the following. You can achieve your desired result by joining all of the items in your array together with </li><li> so that the finished string looks like Pen</li><li>Pencil</li><li>Notebook. This string, once injected into the template's <li> will produce your desired, valid HTML. Note: You will need to use Handlebars' SafeString utility function so as not to escape the HTML tags in the helper's return value.

英文:

I truly do not understand why you cannot use a simple #each as in:

&lt;ul&gt;
  {{#each items}}
    &lt;li&gt;{{this}}&lt;/li&gt;
  {{/each}}
&lt;/ul&gt;

If you absolutely must add your template within the &lt;li&gt;, then you can do the following. You can achieve your desired result by joining all of the items in your array together with &lt;/li&gt;&lt;li&gt; so that the finished string looks like Pen&lt;/li&gt;&lt;li&gt;Pencil&lt;/li&gt;&lt;li&gt;Notebook. This string, once injected into the template's &lt;li&gt; will produce your desired, valid HTML. Note: You will need to use Handlebars' SafeString utility function so as not to escape the HTML tags in the helper's return value.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const template = Handlebars.compile(`&lt;ul&gt;&lt;li&gt;{{#listConstruct product}}{{this}}{{/listConstruct}}&lt;/li&gt;&lt;/ul&gt;`);

const product = [
  &#39;Pen&#39;,
  &#39;Pencil&#39;,
  &#39;Notebook&#39;
];

Handlebars.registerHelper(&#39;listConstruct&#39;, function (items, options) {
  return options.fn(new Handlebars.SafeString(items.join(&#39;&lt;/li&gt;&lt;li&gt;&#39;)));
});

document.body.innerHTML = template({ product });

<!-- 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年3月23日 10:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818898.html
匿名

发表评论

匿名网友

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

确定