func ShowPostTemplate(writer http.ResponseWriter, r *http.Request, slug string) error { // Read lock templates and global blog compiledTemplates.RLock() defer compiledTemplates.RUnlock() methods.Blog.RLock() defer methods.Blog.RUnlock() post, err := database.RetrievePostBySlug(slug) if err != nil { return err } else if !post.IsPublished { // Make sure the post is published before rendering it return errors.New("Post not published.") } requestData := structure.RequestData{Posts: make([]structure.Post, 1), Blog: methods.Blog, CurrentTemplate: 1, CurrentPath: r.URL.Path} // CurrentTemplate = post requestData.Posts[0] = *post // If the post is a page and the page template is available, use the page template if post.IsPage { if template, ok := compiledTemplates.m["page"]; ok { _, err = writer.Write(executeHelper(template, &requestData, 1)) // context = post return err } } _, err = writer.Write(executeHelper(compiledTemplates.m["post"], &requestData, 1)) // context = post if requestData.PluginVMs != nil { // Put the lua state map back into the pool plugins.LuaPool.Put(requestData.PluginVMs) } return err }
func generateUniqueSlug(slug string, table string, suffix int) string { // Recursive function slugToCheck := slug if suffix > 1 { // If this is not the first try, add the suffix and try again slugToCheck = slug + "-" + strconv.Itoa(suffix) } var err error if table == "tags" { // Not needed at the moment. Tags with the same name should have the same slug. _, err = database.RetrieveTagIdBySlug(slugToCheck) } else if table == "posts" { _, err = database.RetrievePostBySlug(slugToCheck) } else if table == "users" { _, err = database.RetrieveUserBySlug(slugToCheck) } if err == nil { return generateUniqueSlug(slug, table, suffix+1) } return slugToCheck }