【问题标题】:problem with formatting date inside velocity template在速度模板中格式化日期的问题
【发布时间】:2021-10-25 09:30:41
【问题描述】:

我正在尝试通过 dotcms 中的速度模板创建站点地图

这是我的代码

#set($articles = $json.fetch('https://xxxxx.xxxx.xxx/api/content/render/false/type/json/query/+contentType:YmArticlePage%20+(conhost:b5d82078-a844-4104-9664-4348c2420bba%20conhost:SYSTEM_HOST)%20+(wfscheme:d61a59e1-a49c-46f2-a929-db2b4bfa88b2*)%20+(wfstep:dc3c9cd0-8467-404b-bf95-cb7df3fbc293*)%20/limit/1/orderby/score,modDate%20desc'))
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    #foreach($article in $articles.contentlets)
    <url>
        <loc>$!{host}$!{article.path}</loc>
        <changefreq>always</changefreq>
        <priority>0.6</priority>
        <lastmod>$date.format('medium', $article.modDate) $article.modDate</lastmod>
    </url>
    #end
</urlset>

我能够渲染 $article.modDate,但如果我要使用 dateViewtool 对其进行格式化,它似乎不会对其进行格式化,只显示字符串“$date.format('medium', $article.modDate)”在浏览器中查看时。

请看下面的截图。

【问题讨论】:

    标签: velocity dotcms


    【解决方案1】:

    dotcms 内容类型 api 将日期作为字符串抛出。所以我必须先将它们转换为日期,然后我才能进行格式化

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        #foreach($article in $articles.contentlets)
        #set($md = $date.toDate("yyyy-MM-dd HH:mm:ss",$article.modDate))
        <url>
            <loc>$!{host}$!{article.path}</loc>
            <changefreq>always</changefreq>
            <priority>0.6</priority>
            <lastmod>$date.format("dd MMM yyyy HH:mm:ss", $md)</lastmod>
        </url>
        #end
    </urlset>
    

    这对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-15
      • 2019-02-17
      • 2016-03-23
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多