立诚勿怠,格物致知
It's all about connecting the dots

Recursion in EJS and Handlebars

Once in a frontend project using handlebars as the template, I can use code like the following code snippet to realize recursion:

<script id="contentTpl" type="text/x-handlebars-template">
  {{! 通过#*inline标签定义行内partials片段,供递归使用}}
  {{#*inline "lists"}}
    {{#if details.length}}
      <ul class="lists">
        {{#each details}}
          <li data-index="{{@index}}" class="list{{getOpenStatus open}}">
            <div class="item-wrapper">
              {{#if details.length}}
                <span class="icon-toggle"></span>
              {{/if}}
              <span class="icon-entity{{getEntityCat details}}">F</span>
              <span class="icon-checkbox{{getCheckStatus checked details}}"></span>
              <span class="name">{{name}}</span>
              {{> lists}}
            </div>
          </li>
        {{/each}}
      </ul>
    {{/if}}
  {{/inline}}
  <article class="trees-wrapper">
    {{> lists}}
  </article>
</script>

So, I think probably recursion effect can also be implemented in EJS template. And after some testing, the answer is yes, and a sample code snippet is as below:

<% (function recursive(arr) { %>
  <% arr.forEach(function (item) { %>
    <span><%= item.text %></span>
    <% if (item.details && item.details instanceof Array) { %>
      <% recursive(item.details) %>
    <% } %>
  <% }) %>
<% })(data) %>
赞(0) 打赏
文章名称:《Recursion in EJS and Handlebars》
文章链接:https://www.orzzone.com/recursion-in-ejs-and-handlebars.html
商业联系:yakima.public@gmail.com

本站大部分文章为原创或编译而来,对于本站版权文章,未经许可不得用于商业目的,非商业性转载请以链接形式标注原文出处。
本站内容仅供个人学习交流,不做为任何投资、建议的参考依据,因此产生的问题需自行承担。

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力提供更多优质内容!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册