Giter Club home page Giter Club logo

Comments (1)

esemplastic avatar esemplastic commented on July 21, 2024 1

Yes @rebootcode because you're trying to use a template function instead of printing something, in golang we use the {{.property_name}} to print a binded value. Even the Iris' README.md overview html and go source code shows that-- https://github.com/kataras/iris/blob/master/README.md#-installation but it's ok, you have to watch the iris project in order to not miss the good tricks in golang+web.

See the first dot(.)

Welcome {{.user.Name}} 

A trivial example:

./main.go

package main

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/sessions"
)

type User struct {
	ID   int64
	Name string
}

func main() {
	app := iris.New()
	app.RegisterView(iris.HTML("./views", ".html"))
	sess := sessions.New(sessions.Config{Cookie: "sessionid"})

	app.Get("/login", func(ctx iris.Context) {
		var testUser = User{Name: "iris"}

		session := sess.Start(ctx)
		session.Set("user", testUser)
	})

	app.Get("/", func(ctx iris.Context) {
		session := sess.Start(ctx)
		testUser := session.Get("user")
		ctx.ViewData("user", testUser)
		ctx.View("index.html")
	})

	app.Run(iris.Addr(":8080"))
}

./views/index.html

<html>

<head>
    <title>Welcome {{.user.Name}}</title>
</head>

<body>
    <h1>Welcome {{.user.Name}} | Logout</h1>
</body>

</html>
  1. http://localhost:8080/login
  2. http://localhost:8080/

Even if it's not a good idea to store the whole user data in session, usually I store just the user's ID and fetch the whole user when I need it.

from examples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.