Handlebars.js自定义条件帮助程序

Handlebars.js Custom Conditional helper

本文关键字:帮助程序 条件 自定义 js Handlebars      更新时间:2023-09-26

我正在使用handlebars.registerHelper.在handlers.js中为自己制作一个自定义助手

我已经注册了以下助手:

Handlebars.registerHelper("determineItemType", function(type){
            console.log("--------> " + type);
                if(type == "document")
                {
                    return "document";
                } else if(type == "audio")
                {
                return "audio";
                }
            });

在Handlebars.js的模板中,我使用它如下:

{{#determineItemType "document"}}
    <img src="icon_document.png"></img>
{{/determineItemType}}

但问题是,它没有显示文档图标。它显示的是单词"文档"代替图标。

以下是页面的完整代码:

<!DOCTYPE html>
<html>
<head>
    <title>Handlebars.js example</title>
</head>
<body>
    <div id="placeholder">This will get replaced by handlebars.js</div>
    <script type="text/javascript" src="handlebars.js"></script>
    <script id="myTemplate" type="text/x-handlebars-template">

        {{#names}}
        <div style="width:100%;border:2px solid red;">
        <table style="width:100%;border:2px solid black">
            <tr>
                 <td style="width:50%; border:2px solid yellow;">
                        <img src="{{itemImage}}"></img>
                </td>
               <td style="width:50%; border:2px solid green;">
                    {{#if isAudioAvailable}}
                            {{#if isAudioDownloaded}}
                                <img src="btn_viewAudio.png"></img><br><br>
                            {{else}}        
                                <img src="btn_downloadAudio.png"></img><br><br>
                            {{/if}}
                        {{/if}}
                        {{#if isPresentationAvailable}}
                            {{#if isPresentationDownloaded}}
                                <img src="btn_viewPresentation.png"></img><br><br>
                            {{else}}
                                <img src="btn_downloadPresentation.png"></img><br><br>
                            {{/if}}
                        {{/if}}
                        {{#if isTranscriptAvailable}}
                            {{#if isTranscriptDownloaded}}
                                <img src="btn_viewTranscript.png"></img><br><br>
                            {{else}}
                            <img src="btn_downloadTranscript.png"></img><br><br>
                            {{/if}}
                        {{/if}}
                      {{#if isVideoAvailable}}
                            {{#if isVideoDownloaded}}
                                <img src="btn_viewVideo.png"></img><br><br>
                            {{else}}
                            <img src="btn_downloadVideo.png"></img><br><br>
                            {{/if}}
                      {{/if}}
               </td>
           </tr>
           <tr>
                <td colspan="2">
                    {{#determineItemType "document"}}
                        <img src="icon_document.png"></img>
                    {{/determineItemType}}
                    &nbsp;
                <label style="font-weight:bolder">{{itemTitle}}</label>
               </td>
           </tr>
           <tr>
            <td colspan="2">
                    <p>{{itemDescription}}</p>
                </td>
           </tr>
        </table>
        </div>  
        {{/names}}
    </script>
    <script type="text/javascript">
        var source = document.getElementById("myTemplate").innerHTML;
        var template = Handlebars.compile(source);
        //alert(template);
        var data = {
            names: [
            { "itemImage": "authorImage.png",
                "itemTitle": "Handlebars.js Templating for HTML",
                "itemType": "document",
                "isAudioAvailable": true,
                "isAudioDownloaded": false,
                "isPresentationAvailable": true,
                "isPresentationDownloaded": false,
                "isTranscriptAvailable": true,
                "isTranscriptDownloaded": true,
                "isVideoAvailable": false,
                "isVideoDownloaded": false,
                "itemDescription": "Rendeting HTML content using Javascript is always messy! Why? The HTML to be rendered is unreadable. Its too complex to manage. And - The WORST PART: It does it again and again and again! Loss: Performance, Memory, the DOM has to be re-drawn again each and every time a tag is added."}
            ]
        };
        Handlebars.registerHelper("determineItemType", function(type){
            console.log("--------> " + type);
                if(type == "document")
                {
                    return "document";
                } else if(type == "audio")
                {
                return "audio";
                }
            });

        document.getElementById("placeholder").innerHTML = template(data);
    </script>

</body>
</html>

感谢您的帮助。

谢谢,安基特。

块助手返回的字符串用作注入到渲染模板中的内容。您返回文档,所以这就是您在HTML中得到的内容。

发件人http://handlebarsjs.com/block_helpers.html

[块助手]将接收一个选项哈希。此选项哈希包含一个函数(options.fn),其行为类似于正常编译的Handlebars模板。具体来说,函数将采用上下文并返回一个字符串。

假设你想针对itemType进行测试,你的助手可以写成

Handlebars.registerHelper("determineItemType", function(type, options){
    return (this.itemType === type) ? options.fn(this) : "";
});

和一个Fiddle一起玩http://jsfiddle.net/NqCFB/