0

我在玉模板中有以下功能:

-function prettyDate(dateString){
    -var date = new Date(dateString);
    -var d = date.getDate();
    -var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
    -var m = monthNames[date.getMonth()];
    -var y = date.getFullYear();
    -return d+' '+m;
-}

并使用以下代码调用:

td #{prettyDate(c.dateStart)}

但我在那条线上遇到了错误:

undefined is not a function

然而,它显然是一个函数。我究竟做错了什么?

如果您有兴趣,这是完整的翡翠模板:

extends ../layout

-function prettyDate(dateString){
-var date = new Date(dateString);
-var d = date.getDate();
-var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
-var m = monthNames[date.getMonth()];
-var y = date.getFullYear();
-return d+' '+m;
-}

block content
    div#highlighted
        div.container
            div.row-fluid.header
                h2.page-title
                    span Courses
    div#content
        div.container.portfolio
            div.row
                div.span7.project-photos
                    each c in course
                        div.block.course-snippet
                            h3.block-title
                                span: a(href='/courses/#{c.shortTitle}') #{c.title}: #{c.type} (ages #{c.ageLow}-#{c.ageHigh})
                            dl
                                dt Dates
                                dd #{c.dateStart} - #{c.dateEnd}
                                dt Tutors
                                dd Names hidden
                                dt Fee
                                dd #{c.fee}
                                dt Course Description
                                dd #{c.shortDescription}
                            a.btn.btn-primary(href="/courses/#{c.shortTitle}") More details
                            |  
                            a.btn.btn-primary(href="/booking/#{c.shortTitle}") Book a place now
                div.span5.sidebar.sidebar-right
                    h3 Full Course Schedule
                    table#courses-table
                        thead
                            tr
                                td Start date
                                td End date
                                td Course
                                td Age range
                                td Fee (£)
                        tbody
                            each c in course
                                tr
                                    td #{prettyDate(c.dateStart)}
                                    td #{c.dateEnd}
                                    td
                                        a(href="/courses/#{c.shortTitle}") #{c.title}
                                    td #{c.ageLow}-#{c.ageHigh}
                                    td #{c.fee}
4

2 回答 2

1

现在已经通过将函数移动到 app.js 文件中的 app.locals 解决了这个问题:

app.locals.prettyDate = function(dateString){
    var date = new Date(dateString);
    var d = date.getDate();
    var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
    var m = monthNames[date.getMonth()];
    var y = date.getFullYear();
    return d+' '+m;
}

如所述,该功能可在模板中使用。

于 2013-06-05T14:25:04.927 回答
0

我发现这个帖子似乎有效。

https://www.filosophy.org/post/34/using_javascript_functions_within_the_jade_templating_language/

将您的函数更改为变量,您应该一切顺利。

-var prettyDate = function (datestring){
于 2018-01-31T15:11:58.427 回答