xml template

This commit is contained in:
aichy126 2022-12-12 16:33:08 +08:00
parent 6459f6b7cb
commit b9db22e9f6
5 changed files with 42 additions and 11 deletions

View File

@ -172,7 +172,7 @@ func NewHTTPServer(debug bool,
r.LoadHTMLGlob("../../ui/template/*")
} else {
html, _ := fs.Sub(ui.Template, "template")
htmlTemplate := template.Must(template.New("").Funcs(funcMap).ParseFS(html, "*.html"))
htmlTemplate := template.Must(template.New("").Funcs(funcMap).ParseFS(html, "*"))
r.SetHTMLTemplate(htmlTemplate)
}

View File

@ -404,12 +404,7 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI
}
func (tc *TemplateController) Sitemap(ctx *gin.Context) {
xml, err := tc.templateRenderController.Sitemap(ctx)
if err != nil {
tc.Page404(ctx)
return
}
ctx.String(http.StatusOK, xml)
tc.templateRenderController.Sitemap(ctx)
}
func (tc *TemplateController) SitemapPage(ctx *gin.Context) {

View File

@ -1,7 +1,8 @@
package templaterender
import (
"fmt"
"html/template"
"net/http"
"github.com/answerdev/answer/internal/schema"
"github.com/gin-gonic/gin"
@ -15,10 +16,32 @@ func (t *TemplateRenderController) QuestionDetail(ctx *gin.Context, id string) (
return t.questionService.GetQuestion(ctx, id, "", schema.QuestionPermission{})
}
func (t *TemplateRenderController) Sitemap(ctx *gin.Context) (string, error) {
return "Sitemap", nil
func (t *TemplateRenderController) Sitemap(ctx *gin.Context) {
//question list page
ctx.Header("Content-Type", "application/xml")
ctx.HTML(
http.StatusOK, "sitemap-list.xml", gin.H{
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"list": "string",
},
)
//question url list
ctx.Header("Content-Type", "application/xml")
ctx.HTML(
http.StatusOK, "sitemap.xml", gin.H{
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"list": "string",
},
)
}
func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) (string, error) {
return fmt.Sprintf("SitemapPage-%d", page), nil
ctx.Header("Content-Type", "application/xml")
ctx.HTML(
http.StatusOK, "sitemap.xml", gin.H{
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"list": "string",
},
)
}

View File

@ -0,0 +1,6 @@
{{ .xmlHeader }}
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/sitemap1.xml</loc>
</sitemap>
</sitemapindex>

7
ui/template/sitemap.xml Normal file
View File

@ -0,0 +1,7 @@
{{ .xmlHeader }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/foo1</loc>
<lastmod>2018-06-04</lastmod>
</url>
</urlset>