车把定制助手卡在"不匹配"

Siy*_*yah 2 javascript handlebars.js

我想通过使用把手使某些元素小写(我知道它可以用CSS,但你不能为类名做这个).无论如何,我收到这个错误:

Uncaught Error: toLowerCase doesn't match each
Run Code Online (Sandbox Code Playgroud)

我的代码:

    <script id="icon-template" type="text/x-handlebars-template">
        {{#each results}}
        <li>
            <div class="content">
                    <i class="Icon icon-{{#toLowerCase contentType}}"></i>
            </div>
       </li>
       {{/each}}
    </script>
Run Code Online (Sandbox Code Playgroud)

自定义助手:

<script type="text/javascript">
Handlebars.registerHelper("toLowerCase", function(input) {
    var output = input.toLowerCase();
    return output.replace(" ", "");
});
</script>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Siy*_*yah 15

我想到了.对于有同样问题的人:

<script type="text/javascript">
handlebars.registerHelper("toLowerCase", function(input) {
    var output = input.toLowerCase();
    return output.replace(" ", "");
});
</script>
Run Code Online (Sandbox Code Playgroud)

句柄必须首先使用小写字母(如下所示handlebars:),并且在使用自定义帮助程序时不需要使用主题标签.所以自定义助手现在toLowerCase而不是#toLowerCase

<script id="icon-template" type="text/x-handlebars-template">
    {{#each results}}
    <li>
        <div class="content">
                <i class="Icon icon-{{toLowerCase contentType}}"></i>
        </div>
   </li>
   {{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)

  • 哪些文档支持这个?我理解函数vs块表达式的问题.只有块表达式在其名称前面带有`#`. (3认同)

小智 15

如果把手助手名称全部为小写:

<script type="text/javascript">
handlebars.registerHelper("lower", function(input) {
    var output = input.toLowerCase();
    return output.replace(" ", "");
});
</script>
Run Code Online (Sandbox Code Playgroud)

你需要在调用时使用哈希:

<script id="icon-template" type="text/x-handlebars-template">
   <i class="Icon icon-{{#lower contentType}}"></i>
</script>
Run Code Online (Sandbox Code Playgroud)

如果帮助程序使用CamelCase名称:

<script type="text/javascript">
handlebars.registerHelper("toLowerCase", function(input) {
    var output = input.toLowerCase();
    return output.replace(" ", "");
});
</script>
Run Code Online (Sandbox Code Playgroud)

然后你不使用哈希:

<script id="icon-template" type="text/x-handlebars-template">
   <i class="Icon icon-{{toLowerCase contentType}}"></i>
</script>
Run Code Online (Sandbox Code Playgroud)