【问题标题】:Handlebars.js Conditional StatementHandlebars.js 条件语句
【发布时间】:2013-07-11 17:05:16
【问题描述】:

我正在尝试在我的 Handlebars 模板中执行条件语句。问题是如果插入了 if 条件,它根本不会呈现内容。

这是我的代码:

<!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;">

                        <img src="btn_downloadAudio.png"></img><br><br>

                        <img src="btn_downloadPresentation.png"></img><br><br>
                      <img src="btn_downloadTranscript.png"></img><br><br>
                      <img src="btn_downloadVideo.png"></img><br><br>
               </td>
           </tr>
           <tr>
                <td colspan="2"><img src="{{itemType}}">&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": "icon_document.png",
                "isAudioAvailable": "true",
                "isPresentationAvailable": "true",
                "isTranscriptAvailable": "true",
                "isVideoAvailable": "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."}
            ]
        };

        document.getElementById("placeholder").innerHTML = template(data);

    </script>

</body>
</html>

条件:如果 isVideoAvailable 为 true,则显示视频按钮

感谢任何帮助。

谢谢, Ankit

【问题讨论】:

  • 似乎工作正常:jsfiddle.net/ambiguous/DAHKY
  • @muistooshort 是的,渲染很好。我只想在“isVideoAvailable”==“true”时渲染视频按钮。如果它是假的,我想隐藏它。这个可以吗?

标签: javascript html templates handlebars.js


【解决方案1】:

如果您希望有条件地显示某些内容,请使用{{#if}}

{{#if isVideoAvailable}}
    <img src="btn_downloadVideo.png"><br><br>
{{/if}}

当然,要使其正常工作,您的数据应该是有意义的,isVideoAvailable 应该是一个布尔值。因此,您还需要清理数据以使其有意义,isVideoAvailable 应该是 truefalse 而不是字符串 'true''false';预处理数据在 Handlebars 中很常见,因此修复数据是最好的做法,修复数据还可以让您在 JavaScript 中以自然的方式使用它。

演示:http://jsfiddle.net/ambiguous/LjFr4/

但是,如果您坚持将布尔值保留为字符串,那么您可以添加一个 if_eq 助手并说:

{{#if_eq isVideoAvailable "true"}}
    <img src="btn_downloadVideo.png"><br><br>
{{/if_eq}}

清理您的数据会是一个更好的主意。

【讨论】:

  • 我无法将 if_eq 帮助器用于字符串类型。它适用于布尔值,但不适用于字符串类型的数据。 :(
  • @AnkitTanna:当您将if_eq 与字符串一起使用时发生了什么?您是如何使用它的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 2013-03-09
  • 2011-05-29
  • 2012-07-16
  • 2011-10-29
相关资源
最近更新 更多