本文整理匯總了Golang中github.com/gin-gonic/gin.Context.HTML方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.HTML方法的具體用法?Golang Context.HTML怎麽用?Golang Context.HTML使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.HTML方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RenderRegisterForm
func RenderRegisterForm(ctx *gin.Context, result *RegisterResult, user *SiteUser) {
tplData := gin.H{
"title": "Automata Theory - Lab 1, form validation",
"alertMessage": "",
"showAlertName": false,
"showAlertEmail": false,
"nickname": "",
"email": "",
}
if result != nil && result.status != UserValid {
tplData["alertMessage"] = result.message
switch result.status {
case UserInvalidNickname:
tplData["showAlertName"] = true
case UserInvalidEmail:
tplData["showAlertEmail"] = true
case UserWeakPassword:
tplData["showAlertPassword"] = true
case UserPasswordMismatch:
tplData["showAlertPassword"] = true
}
}
if user != nil {
tplData["nickname"] = user.nickname
tplData["email"] = user.email
}
ctx.HTML(http.StatusOK, "reg-form.tpl", tplData)
}
示例2: HomeR
func HomeR(c *gin.Context) {
// dependency injection
var (
whiskyStore = c.MustGet(WhiskyStoreKey).(WhiskyStore)
reviewStore = c.MustGet(ReviewStoreKey).(ReviewStore)
user = GetUser(c)
)
// temporary variables
var (
activity []Review
trending []Whisky
err error
)
// get activity stream for user
activity, err = reviewStore.GetActivity(user.ID, LIMIT)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// get trending whiskies
trending, err = whiskyStore.GetTrending(LIMIT)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.HTML(http.StatusOK, "home.html", gin.H{
"User": user,
"Activity": activity,
"Trending": trending,
})
}
示例3: Index
func (h *WebHandler) Index(c *gin.Context) {
res, rec := []Result{}, []algorithm.Record{}
e := h.DB.Limit(10).Order("idx desc").Find(&rec).Error
if e != nil {
c.HTML(http.StatusOK,
"index",
gin.H{
"title": "NICE",
"results": "",
"error": fmt.Sprintf("Error occured[%s]", e.Error()),
},
)
return
}
//bkt := base.NewBucket(false)
//fmt.Println("xxxx:",len(bkt.Balls))
for k, _ := range rec {
res = append(res, rec[k].LoadResult())
}
c.HTML(http.StatusOK,
"index",
gin.H{
"title": "NICE",
"results": res,
"error": "",
},
)
}
示例4: EditWhiskyFormR
func EditWhiskyFormR(c *gin.Context) {
// dependency injection
var (
whiskyStore = c.MustGet(WhiskyStoreKey).(WhiskyStore)
user = GetUser(c)
)
// only authenticated users can perform this action
if !user.Authenticated() {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
// fetch whisky to edit
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
whisky, err := whiskyStore.GetWhisky(id)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.HTML(http.StatusOK, "whisky-form.html", gin.H{
"Action": "Edit",
"User": user,
"Form": whisky,
"Errors": map[string]string{},
})
}
示例5: UserR
func UserR(c *gin.Context) {
// dependency injection
var (
whiskyStore = c.MustGet(WhiskyStoreKey).(WhiskyStore)
reviewStore = c.MustGet(ReviewStoreKey).(ReviewStore)
user = GetUser(c)
)
// parse id
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
// fetch whiksy
whisky, err := whiskyStore.GetWhisky(id)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// fetch reviews
reviews, err := reviewStore.GetAllReviews(id, 30)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.HTML(http.StatusOK, "whisky.html", gin.H{
"User": user,
"Whisky": whisky,
"Reviews": reviews,
})
}
示例6: New
func (self *EnvController) New(c *gin.Context) {
username := self.CurrentUser(c)
if username == "" {
c.Redirect(http.StatusFound, "/")
return
}
appName := c.Param("appName")
key := c.PostForm("key")
value := c.PostForm("value")
err := env.Create(self.etcd, username, appName, key, value)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
"alert": true,
"error": true,
"message": "Failed to add environment variable.",
})
return
}
c.Redirect(http.StatusSeeOther, "/apps/"+appName)
}
示例7: HomeCtr
func (fc *FrontController) HomeCtr(c *gin.Context) {
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
if err != nil {
log.Fatal(err)
}
page -= 1
if page < 0 {
page = 0
}
prev_page := page
if prev_page < 1 {
prev_page = 1
}
next_page := page + 2
rpp := 20
offset := page * rpp
CKey := fmt.Sprintf("home-page-%d-rpp-%d", page, rpp)
var blogList string
val, ok := Cache.Get(CKey)
if val != nil && ok == true {
fmt.Println("Ok, we found cache, Cache Len: ", Cache.Len())
blogList = val.(string)
} else {
rows, err := DB.Query("Select aid, title from top_article where publish_status = 1 order by aid desc limit ? offset ? ", &rpp, &offset)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var (
aid int
title sql.NullString
)
for rows.Next() {
err := rows.Scan(&aid, &title)
if err != nil {
log.Fatal(err)
}
blogList += fmt.Sprintf(
"<li><a href=\"/view/%d\">%s</a></li>",
aid,
title.String,
)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
Cache.Add(CKey, blogList)
}
session := sessions.Default(c)
username := session.Get("username")
c.HTML(http.StatusOK, "index.html", gin.H{
"bloglist": template.HTML(blogList),
"username": username,
"prev_page": prev_page,
"next_page": next_page,
})
}
示例8: Login
// Login is a page with a login form and an alternative to the login API,
// this route handles both GET and POST requests.
func Login(c *gin.Context) {
session := sessions.Default(c)
defer session.Save()
// returnURL can come from GET or POST or use default.
returnURL := c.DefaultQuery("return_url", c.DefaultPostForm("return_url", "/"))
if c.Request.Method == "POST" {
var schema LoginSchema
if c.Bind(&schema) == nil {
// Fetch the user matching this username.
user := GetUserByUsername(schema.Username)
// If the user exists, the ID is > 0, check the password.
if user.ID > 0 && user.CheckPassword(schema.Password) {
session.Set("userID", user.ID)
c.Redirect(http.StatusFound, returnURL)
return
}
session.AddFlash("Invalid username or password")
}
}
c.HTML(200, "login.html", pongo2.Context{
"title": "Login",
"messages": session.Flashes(),
"csrf_token": nosurf.Token(c.Request),
"return_url": returnURL,
})
}
示例9: viewPage
func (c *Convergence) viewPage(ctx *gin.Context) {
key := ctx.Param("key")
title := ctx.Param("title")
var err error
var page *Page
if _, err := strconv.Atoi(title); err == nil {
page, err = c.Confluence.GetPageByID(key, title)
if err != nil {
c.showError(ctx, err)
return
}
}
if page == nil {
page, err = c.Confluence.GetPageByTitle(key, title)
if err != nil {
c.showError(ctx, err)
return
}
}
ctx.HTML(http.StatusOK, "page.html", gin.H{
"Title": page.Title,
"Body": c.processBody(page.Body, key),
"Index": key,
})
}
示例10: postRoute
func postRoute(c *gin.Context) {
key := c.Params.ByName("key")
if len(key) < 2 {
four04(c, "Invalid Post")
return
}
key = key[1:]
var ps []Itm
r := Items().Find(bson.M{"key": key}).Sort("-date").Limit(1)
r.All(&ps)
if len(ps) == 0 {
four04(c, "Post not found")
return
}
var posts []Itm
results := Items().Find(bson.M{"date": bson.M{"$lte": ps[0].Date}}).Sort("-date").Limit(20)
results.All(&posts)
obj := gin.H{"title": ps[0].Title, "posts": posts, "channels": AllChannels()}
c.HTML(200, "full.html", obj)
}
示例11: channelRoute
func channelRoute(c *gin.Context) {
key := c.Params.ByName("key")
if len(key) < 2 {
four04(c, "Channel Not Found")
return
}
key = key[1:]
fmt.Println(key)
var posts []Itm
results := Items().Find(bson.M{"channelkey": key}).Sort("-date").Limit(20)
results.All(&posts)
if len(posts) == 0 {
four04(c, "No Articles")
return
}
var currentChannel Chnl
err := Channels().Find(bson.M{"key": key}).One(¤tChannel)
if err != nil {
if string(err.Error()) == "not found" {
four04(c, "Channel not found")
return
} else {
fmt.Println(err)
}
}
obj := gin.H{"title": currentChannel.Title, "header": currentChannel.Title, "posts": posts, "channels": AllChannels()}
c.HTML(200, "full.html", obj)
}
示例12: New
func (self *AppController) New(c *gin.Context) {
username := self.CurrentUser(c)
if username == "" {
c.Redirect(http.StatusFound, "/")
return
}
appName := c.PostForm("appName")
err := app.Create(self.etcd, username, appName)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "apps.tmpl", gin.H{
"alert": true,
"error": true,
"message": "Failed to create app.",
})
return
}
c.Redirect(http.StatusSeeOther, "/apps/"+appName)
}
示例13: Index
// frontpage when no routes are possible.
func Index(c *gin.Context) {
// this is how gin API renders
// response. c.HTML will render
// go template for us.
// gin.H is a shorthand for map[string]interface{}
c.HTML(200, "index.html", gin.H{})
}
示例14: ShowIndex
func ShowIndex(c *gin.Context) {
user := session.User(c)
if user == nil {
c.Redirect(http.StatusSeeOther, "/login")
return
}
// get the repository list from the cache
repos, err := cache.GetRepos(c, user)
if err != nil {
c.String(400, err.Error())
return
}
// filter to only show the currently active ones
activeRepos, err := store.GetRepoListOf(c, repos)
if err != nil {
c.String(400, err.Error())
return
}
c.HTML(200, "index.html", gin.H{
"User": user,
"Repos": activeRepos,
})
}
示例15: searchRoute
func searchRoute(c *gin.Context) {
q := c.Params.ByName("query")
if len(q) < 2 {
four04(c, "Query is too short. Please try a longer query.")
return
}
q = q[1:]
channels := AllChannels()
var posts []Itm
// TODO need to send a PR to Gustavo with support for textscore sorting
results := Items().Find(bson.M{"$text": bson.M{"$search": q}}).Skip(Offset(c)).Limit(pLimit)
results.All(&posts)
if len(posts) == 0 {
four04(c, "No Articles for query '"+q+"'")
return
}
obj := gin.H{"title": q, "header": q, "items": posts, "posts": posts, "channels": channels}
if strings.ToLower(c.Request.Header.Get("X-Requested-With")) == "xmlhttprequest" {
c.HTML(200, "items.html", obj)
} else {
c.HTML(200, "full.html", obj)
}
}