无法使用' date' docpad.coffee里面的元数据

时间:2014-03-11 21:07:37

标签: javascript coffeescript docpad

我正在尝试在docpad.coffee中编写一个日期处理函数,除了使用document.mdate而不是post元数据部分中定义的document.date之外,一切都很好。

docpad.coffee

getFormattedDate: ->
  if @document.date
    date = new Date(@document.date)
    year = date.getFullYear()
    monthnumber = date.getMonth()
    day = date.getDate()
    switch monthnumber
      when 0 then monthtext = 'January'
      when 1 then monthtext = 'February'
      when 2 then monthtext = 'March'
      when 3 then monthtext = 'April'
      when 4 then monthtext = 'May'
      when 5 then monthtext = 'June'
      when 6 then monthtext = 'July'
      when 7 then monthtext = 'August'
      when 8 then monthtext = 'September'
      when 9 then monthtext = 'October'
      when 10 then monthtext = 'November'
      when 11 then monthtext = 'December'
    return monthtext + ' ' + day + ', ' + year

布局文件

<% posts = @getCollection('blog_posts').toJSON() %>

<% for post in posts: %>
  <article>
    <h1><a href="<%= post.url %>"><%= post.title %></a></h1>
    <p class="subtitle"><%= post.subtitle %></p>
    <date><%= @getFormattedDate(post.date) %></date>
  </article>
<% end %>

帖子文件中的元数据

---
layout: 'default'
title: 'Test Blog Post'
subtitle: 'This is the post subtitle'
date: '2014-02-24'
---

HTML输出

<article>
  <h1><a href="/blog/2014-02-24-another-test-blog-post.html">Another Test Blog Post</a></h1>
  <p class="subtitle">This is another post subtitle, it is good.</p>
  <date>March 11, 2014</date>
</article>

我是CoffeeScript的新手。这可能不是编写此函数的最佳方法。我可能最终会将docpad.coffee文件转换为js文件。

无论如何,如何让我的getFormattedDate函数使用日期而不是文件修改日期的文档元数据?谢谢!

0 个答案:

没有答案
相关问题