【问题标题】:Assigning a custom field date to a variable in LocomotiveCMS将自定义字段日期分配给 LocomotiveCMS 中的变量
【发布时间】:2012-08-31 23:03:12
【问题描述】:

我正在尝试将日期分配给变量,以分解布局。也许有更好的方法可以做到这一点,所以请随意推荐替代方案。

我有一个 news_items 模型,有一个名为 news_date 的日期字段。我想浏览每个模型条目,并在遇到新的一年时开始一个新的部分。我的计划很基本:

{% assign curYear = "" %}
{% for news in contents.news_items %}
  {% assign prevYear = curYear %}  
  {% assign curYear = news.news_date.year %} <-- this does not work
  {% if prevYear != curYear %}
    <h1>Press Releases for {{ news.news_date | format_date: '%Y' }}</h1>
  {% endif %}
  <p>{{curYear}}</p> <-- this is always empty
  <p>{{news.content}}</p>
{% endfor %}

我尝试了各种其他语法,例如Time.parseTime(news.news_date).year,但似乎您无法在 Liquid 中执行任意 Ruby。有什么方法可以实现我想要的吗?

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby liquid locomotivecms


    【解决方案1】:

    感谢 google 组中的一位乐于助人的人,向我指出了 capture 标签,它将原本会在页面上输出的内容捕获到变量中:

    而不是这个(或我尝试使用assign 的各种迭代):

    {% assign curYear = news.news_date.year %} 
    

    这很好用,利用format_date 过滤器:

    {% capture curYear %} {{ news.news_date | format_date: '%Y' }} {% endcapture %}
    

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多