Javascript 无法使用';日期';docpad.coffee内部函数中的元数据

Javascript 无法使用';日期';docpad.coffee内部函数中的元数据,javascript,coffeescript,docpad,Javascript,Coffeescript,Docpad,我正在尝试在docpad.coffee内部编写一个日期处理函数,除了它使用document.mdate而不是post metadata部分中定义的document.date之外,一切正常 咖啡 布局文件 HTML输出 这是另一个帖子字幕,很好 2014年3月11日 我不熟悉咖啡脚本。这可能不是编写此函数的最佳方式。我可能最终会将docpad.coffee文件转换为js文件 无论如何,如何使getFormattedDate函数使用文档元数据作为日期而不是文件修改日期?谢谢 我对docpad一无

我正在尝试在docpad.coffee内部编写一个日期处理函数,除了它使用document.mdate而不是post metadata部分中定义的document.date之外,一切正常

咖啡 布局文件 HTML输出

这是另一个帖子字幕,很好

2014年3月11日
我不熟悉咖啡脚本。这可能不是编写此函数的最佳方式。我可能最终会将docpad.coffee文件转换为js文件


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

我对docpad一无所知,但您正在向函数传递一个日期(
@getFormattedDate(post.date)
),但是
getFormattedDate
使用了
@document.date
并忽略了您发送的日期,这似乎有点奇怪。此外,您还可以用一个简单的月份名称数组替换
开关monthnumber
。我去掉了coffescript的混淆,这样我就可以看到发生了什么,并且日期没有被传递到函数中。谢谢@muistooshort我认为你的评论是转换为答案的候选(也许tmbritton会这样标记)+1用于开关建议。Switch语句在js中非常冗余。我通常在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'
---
<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>