javascript 车把自定义助手卡在“不匹配每个”上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32268661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:03:00  来源:igfitidea点击:

Handlebar custom helper stuck on "doesn't match each"

javascripthandlebars.js

提问by Siyah

I want to make certain elements lowercase by using handlebars (I know it's possible with CSS, but you can't do that for classnames for example). Anyhow, I am getting this error:

我想通过使用把手使某些元素小写(我知道使用 CSS 是可能的,但是对于例如类名你不能这样做)。无论如何,我收到此错误:

Uncaught Error: toLowerCase doesn't match each

My code:

我的代码:

    <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>

Custom helper:

自定义助手:

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

What am I doing wrong?

我究竟做错了什么?

回答by Siyah

I figured it out. For anyone having the same problems:

我想到了。对于任何有同样问题的人:

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

Handlebarsmust have a lowercase letter (like this: handlebars) at first & no hashtag is needed when you are using a custom helper. So the custom helper is now toLowerCaseinstead of #toLowerCase

把手首先必须有一个小写字母(像这样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>

回答by otheroom

If a handlebars helper name is all lowercase:

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

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

you will need to use the hash when invoking it:

调用它时,您将需要使用散列:

<script id="icon-template" type="text/x-handlebars-template">
   <i class="Icon icon-{{#lower contentType}}"></i>
</script>

If the helper uses a CamelCase name:

如果助手使用 CamelCase 名称:

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

then you do not use the hash:

那么你不使用哈希:

<script id="icon-template" type="text/x-handlebars-template">
   <i class="Icon icon-{{toLowerCase contentType}}"></i>
</script>

回答by Phil

It matters how you invoke the helper. If you use a hash(#) then it's considered a Block Helperand needs to be closed. Otherwise you'll get that parsing error.

如何调用助手很重要。如果您使用 hash(#) 那么它被认为是一个Block Helper并且需要关闭。否则你会得到那个解析错误。

{{#toLowerCase}}Some UPPERCASE text{{/toLowerCase}}

Obviously it also matters what the helper code does. The syntax above is correct but the code may not have the desired effect.

显然,辅助代码的作用也很重要。上面的语法是正确的,但代码可能没有达到预期的效果。