如何在mustache模板中添加注释?

25

在我的Mustache模板中,我想注释几行代码,但无法实现。我仍然会在HTML中看到注释内容。如何正确添加注释?有人能帮我解决吗?

以下是我的代码:

<script type="text/html" id="inspector-splitViewBase">
    <div class="inspector-split-view-container flex-1 flex-fill flex-down">
        <header class='split-view-inspector-header'>
            <div class="view-title">Source Assets</div>
            {{!--  <div class="actions"> commented 
                <span class="label">Actions</span>
                <span class="gear"></span>
            </div> --}} - comment is not working
        </header>
        <div class='search-container'>
            <span class="search-icon"></span>
            <input type="text" value="" class="inspector-search" />
        </div>
        <div class="source-assets-list-container flex-1"></div>
        <footer></footer>
    </div>
</script>

1
你尝试过在每行代码前加 {{! }} 注释,而不是用多行注释吗? - rivarolle
我尝试过了,但是没有起作用。 - 3gwebtrain
您可以注释整个 HTML 元素,这样它就不会显示出来(使用 <!-- -->)。 - Khalid Dabjan
1
使用您的示例,这对我完美地起作用。模板可以被缓存吗? - rivarolle
2个回答

34

Mustache 文档建议使用以下形式进行注释:

Comments begin with a bang and are ignored. The following template:
    <h1>Today{{! ignore me }}.</h1>

Will render as follows:    
    <h1>Today.</h1>

Comments may contain newlines.

我假设在你的情况下,你必须使用

{{! blah }}

不是

{{!--  blah --}}

1
你正在尝试对既包含mustache模板又包含html的内容进行评论,因此必须在mustache代码处添加mustache注释,在html代码处添加html注释。

.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接