简体   繁体   English

如何将自定义助手 function 作为上下文传递给车把模板?

[英]How to pass custom helper function as a context to handlebars template?

I am new to handlebars and I want to know how to pass a custom helper as a context to the views template ( if that is possible )?我是车把新手,我想知道如何将自定义助手作为上下文传递给视图模板(如果可能的话)? The case is: I have an array of 5 elements and a template with 5 tags, I want to render the template in such a way that it is in the order of the array passed.情况是:我有一个由 5 个元素组成的数组和一个带有 5 个标签的模板,我想按照传递数组的顺序来呈现模板。 Example: if I pass an array as [2,1,3,4,5] then I want the divs to be rendered in that order;示例:如果我将数组作为 [2,1,3,4,5] 传递,那么我希望按该顺序呈现 div; that is second div, then first div, then third div, then fourth div and then the fifth div.这是第二个div,然后是第一个div,然后是第三个div,然后是第四个div,然后是第五个div。 I declared the helper function in the controller file as我在 controller 文件中将助手 function 声明为

Handlebars.RegisterHelper('check',(a,b)=>{
     return a.toString()===b  
})

My handlebars file is like this:我的车把文件是这样的:

<html>
.
.
.
<div id='one'></div>
<div id='two'></div>
<div id='three'></div>
<div id='four'></div>
<div id='five'></div>
.
.
.
</html>

My question is when I pass an array [2,1,5,4,3], I want to render the div in that order that is div with id 'two' then div with id 'one' and like that.我的问题是,当我传递一个数组 [2,1,5,4,3] 时,我想按照 id 为“二”的 div 然后是 id 为“一”的 div 的顺序渲染 div,等等。

I think that if I had more context around what you want to achieve then I might suggest an alternative approach.我认为,如果我对您想要实现的目标有更多的了解,那么我可能会建议一种替代方法。 However, based on the information you have given, I do have a suggested approach.但是,根据您提供的信息,我确实有一个建议的方法。

I think you should not use a helper, but instead use Handlebars Partials for each of the unique div templates you want to define.我认为您应该使用帮助程序,而应为要定义的每个唯一 div 模板使用Handlebars Partials

Then, in the context object you pass to your template, you can supply an array of the partials names you want to include, in the order in which you want to include them.然后,在您传递给模板的上下文 object 中,您可以按照您想要包含它们的顺序提供您想要包含的部分名称的数组。

Assuming you have registered partials named "one", "two", "three", "four", "five", your array might look like ["two", "one", "three", "four", "five"] .假设您注册了名为“一”、“二”、“三”、“四”、“五”的部分,您的数组可能看起来像["two", "one", "three", "four", "five"]

Your template would then iterate through each partial name and use Dynamic Partial resolution to include the correct partial.然后,您的模板将遍历每个部分名称并使用 动态部分解析来包含正确的部分。

Handlebars will not let you to directly pass a variable containing the name to the dynamic partial resolution, so you will need to a lookup . Handlebars 不允许您直接将包含名称的变量传递给动态部分分辨率,因此您需要lookup Assuming the array of partial names is given the identifier partials , the resulting template would look like:假设部分名称数组的标识符为partials ,生成的模板将如下所示:

{{#each partials}}
    {{> (lookup ../partials @index)}}
{{/each}}

I have created a fiddle for your reference.我创建了一个小提琴供您参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM