首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Handlebars.js自定义条件助手

Handlebars.js自定义条件助手
EN

Stack Overflow用户
提问于 2013-07-12 07:44:52
回答 1查看 3.2K关注 0票数 0

我正在用handlebars.js和Handlebars.registerHelper为自己做一个自定义助手。

我已登记下列帮手:

代码语言:javascript
复制
Handlebars.registerHelper("determineItemType", function(type){
            console.log("--------> " + type);

                if(type == "document")
                {
                    return "document";
                } else if(type == "audio")
                {
                return "audio";
                }

            });

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

代码语言:javascript
复制
{{#determineItemType "document"}}
    <img src="icon_document.png"></img>
{{/determineItemType}}

但问题是,它没有显示文档图标。它显示的是“文档”一词而不是图标。

下面是页面的全部代码:

代码语言:javascript
复制
<!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>

任何帮助都是非常感谢的。

谢谢,安吉。

EN

回答 1

Stack Overflow用户

发布于 2013-07-14 06:55:32

块助手返回的字符串用作要注入到呈现模板中的内容。返回文档,这就是HTML中的内容。

来自helpers.html

块助手将收到一个选项散列。此选项散列包含一个函数(options.fn),该函数的行为与普通编译的工具栏模板类似。具体来说,函数将接受上下文并返回一个字符串。

假设您想要对itemType进行测试,则您的助手可以编写为

代码语言:javascript
复制
Handlebars.registerHelper("determineItemType", function(type, options){
    return (this.itemType === type) ? options.fn(this) : "";
});

和一个玩http://jsfiddle.net/NqCFB/的游戏

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17609986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档