【问题标题】:jQuery tmpl plugin - Conditional templates?jQuery tmpl 插件 - 条件模板?
【发布时间】:2011-05-11 16:27:01
【问题描述】:

我目前正在使用jQuery tmpl plugin 生成一个列表:

<script id="mainTemplate" type="text/x-jquery-tmpl">
 <li>
  <a class="Page" href="#" rel="${LINK}" >${DESCRIPTION}</a>
 </li>
</script>

是否有可能使用 jQuery 模板有条件地处理事情?我试过了:

<script id="mainTemplate" type="text/x-jquery-tmpl">
 <li>
 {{if ${TYPE} == "INTLINK" }}
  <a class="Page" href="#" rel="${LINK}" >${DESCRIPTION}</a>
 {{/if}}
 </li>
</script>

但它没有用。我希望能够有条件地根据传递的数据(通过 json)有不同的模板输出。

【问题讨论】:

    标签: jquery templates jquery-templates


    【解决方案1】:

    不需要在 if 标记中包含 TYPE。这应该工作

    <script id="mainTemplate" type="text/x-jquery-tmpl">
        <li>
            {{if TYPE == "INTLINK" }}
                <a class="Page" href="#" rel="${LINK}" >${DESCRIPTION}</a>
            {{/if}}
        </li>
    </script>
    

    假设您的数据包含类似 {"TYPE": "INTLINK"} 的内容。

    【讨论】:

      猜你喜欢
      • 2012-10-21
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 2011-08-18
      相关资源
      最近更新 更多