简体   繁体   中英

problem with formatting date inside velocity template

im trying to create a sitemap via velocity template in dotcms

this is my code

#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>

i am able to render the $article.modDate, but if i am to format it using dateViewtool it doesnt seem to format it and just display the string "$date.format('medium', $article.modDate)" when viewed in the browser.

see screenshot below.

在此处输入图片说明

the dotcms content type api is throwing the dates as string. So i have to convert them first into dates and then i can do the formatting

<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>

this works for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM