【问题标题】:If clause in haml using variable from mustachehaml 中的 if 子句使用来自 mustache 的变量
【发布时间】:2015-02-15 15:22:03
【问题描述】:

类似于this问题,我想在haml文件中使用if子句,具体只在实际有图片url时才显示图片。代码是

%div{ :class => "attachment {{ attachment_type }}" }
  %a{ :href => "{{ attachment_url }}", :target=> "_blank"}
    %img.image{ :src => "{{ attachment_pic_url }

我正在尝试类似:

:plain
<%if {{attachment_pic_url}}.present? %>
  <% { %>
    <img class="image" src="{{ attachment_pic_url}}"  %> %>"/>
  <% } %>

但它抱怨“未关闭的标签”。对这个问题有任何想法吗?或者有更好的方法吗?

【问题讨论】:

  • src="{{ attachment_pic_url %&gt; %&gt;"src="{{ attachment_pic_url }}"

标签: ruby haml mustache


【解决方案1】:

Mustache 没有逻辑,因为没有 if 语句。

我们可以使用标签来代替。

在你的情况下 -

{{#attachment_pic_url}}
%a{ :href => "{{ attachment_url }}", :target=> "_blank"}
  %img.image{ :src => "{{ attachment_pic_url }}" }
{{/attachment_pic_url}}

如果attachment_key_url键不存在,或存在且值为null、undefined、false、0或NaN,或者为空字符串或空列表,则不会渲染块。

请参考https://github.com/janl/mustache.js#false-values-or-empty-lists

【讨论】:

  • 结束标签应该是文档中的 {{/attachment_pic_url}}。它有助于解决问题。谢谢@Shweta!
猜你喜欢
  • 1970-01-01
  • 2014-04-11
  • 2014-05-05
  • 1970-01-01
  • 2014-07-19
  • 2016-07-02
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多