func Pager(total int, page int, pagesize int) template.HTML { str := "" str += "<ul class=\"pagination\">" prev, next, totalPage := page-1, page+1, total/pagesize if total%pagesize != 0 { totalPage++ } if prev <= 0 { str += "<li class=\"disabled\"><a href=\"/admin/users?page=" + fmt.Sprintf("%d", prev) + "\">«</a></li>" } else { str += "<li><a href=\"/admin/users?page=" + fmt.Sprintf("%d", prev) + "\">«</a></li>" } for p := 0; p < totalPage; p++ { link := fmt.Sprintf("/admin/users?page=%d", p+1) if p == page-1 { str += "<li class=\"active\"><a href=\"" + link + "\">" + fmt.Sprintf("%d", p+1) + "</a></li>" } else { str += "<li><a href=\"" + link + "\">" + fmt.Sprintf("%d", p+1) + "</a></li>" } } if next > totalPage { str += "<li class=\"disabled\"><a href=\"/admin/users?page=" + fmt.Sprintf("%d", next) + "\">«</a></li>" } else { str += "<li><a href=\"/admin/users?page=" + fmt.Sprintf("%d", next) + "\">«</a></li>" } str += "</ul>" return beego.Str2html(str) }
func news(in string) (out template.HTML) { out = beego.Str2html(in) return }
func MarkdownHtml(s string) template.HTML { return beego.Str2html(string(Markdown([]byte(s)))) }
// 添加文章内容 func (this *TopicController) AddTopic() { flash := beego.NewFlash() if checkAccountSession(&this.Controller) { //验证用户是否已登录 topic := &models.Topic{} err := this.ParseForm(topic) if err != nil { beego.Error("添加文章内容失败:" + err.Error()) flash.Error("添加文章失败!") flash.Store(&this.Controller) this.Redirect("/", 302) return } topic.Created = time.Now().Local() //设置创建时间 topic.Updated = time.Now().Local() //设置更新时间 if this.GetSession("user") != nil { topic.Author = this.GetSession("user").(*models.User).Nickname //设置作者 topic.Username = this.GetSession("user").(*models.User).Username //设置用户名 } if nil == models.GetCategoryByName(topic.Category) { //查询该分类是否存在 //不存在,则保存该分类 if !models.AddCategory(topic.Category) { beego.Error("添加文章分类失败:" + err.Error()) flash.Error("添加文章分类失败!") flash.Store(&this.Controller) } } err = models.AddTopic(topic) if err != nil { beego.Error("添加文章失败:" + err.Error()) flash.Error("添加文章失败!") flash.Store(&this.Controller) this.Redirect("/topic/add", 302) return } staticpath, err := filepath.Abs("html/" + strconv.FormatInt(topic.Id, 10) + ".html") if err != nil { log.Print("获取文件的物理路径失败:" + err.Error()) } file, err := os.Create(staticpath) if err != nil { log.Print("创建文件失败:" + err.Error()) } // tp1, err := filepath.Abs("views/header.tpl") // tp2, err := filepath.Abs("views/view_topic.html") // tp3, err := filepath.Abs("views/footer.tpl") // tp4, err := filepath.Abs("views/msg.tpl") // tp5, err := filepath.Abs("views/nav.tpl") if err != nil { log.Print("读取模板失败:" + err.Error()) } tplFuncMap := make(template.FuncMap) tplFuncMap["dateformat"] = beego.DateFormat //注册模板中使用到的模板函数dateformat // tplFuncMap["str2html"] = beego.Str2html //注入模板中使用到的模板函数str2html t := template.New("view_topic.html") //此处的view_topic.html为具体的模板名称 t = t.Funcs(tplFuncMap) /*.ParseFiles(tp1, tp2, tp3, tp4, tp5)*/ t, err = t.ParseGlob("views/*") //模板存放路径 将会匹配到views/目录下的所有文件 if err != nil { log.Print("解析模板失败:" + err.Error()) } data := map[string]interface{}{ "Title": "title", "Topic": topic, "Time": time.Now().Local(), "Generated": beego.Str2html("<!-- 本页创建于" + beego.DateFormat(time.Now().Local(), "2006-01-02 15:04:05") + " -->"), } err = t.Execute(file, data) if err != nil { log.Print("解析模板失败:" + err.Error()) } this.Redirect("/", 302) //添加成功到文章列表页 return } else { flash.Error("您尚未登录,请登录!") flash.Store(&this.Controller) this.Redirect("/login", 302) //跳转到登录页 return } }