Golang Context.HTML Examples

Golang Context.HTML - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.HTML extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
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)
}
Example #2
0
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,
	})
}
Example #3
0
func (pc *NodeController) postNodeAction(c *gin.Context) {

	id := c.Param("id")

	var form models.NodeUpdateForm
	if err := c.Bind(&form); err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	node, err := models.NodeMapper.FetchOneById(id)
	if err != nil {
		c.HTML(http.StatusInternalServerError, "error_500.html", map[string]interface{}{
			"error": err,
		})
		return
	}

	if node == nil {
		c.HTML(http.StatusNotFound, "error_404.html", map[string]interface{}{
			"text": "Node not found",
		})
		return
	}

	node.Tags = form.Tags
	node.Description = form.Description
	models.NodeMapper.Update(node)

	c.Redirect(http.StatusFound, "/nodes")
}
Example #4
0
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{},
	})
}
Example #5
0
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,
	})
}
Example #6
0
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)
}