如何使用Revel / Golang从控制器设置持久性全局模板变量

时间:2015-01-11 23:30:53

标签: go revel

我是Golang的新手,我正在从Node.js服务器切换到Golang服务器,我正在尝试重写我之前为Node编写的应用程序。

我想在用户登录时设置模板变量,但我不确定如何,是的,我尝试使用Google搜索。

这是我的注册用户控制器:

func (c User) RegisterUser(user_email, user_password,
    user_password_confirmation, user_first_name,
    user_last_name string) revel.Result {

    // Validate Email
    c.Validation.Required(user_email).Message("Username is required")
    c.Validation.Email(user_email).Message("Email is not a valid email")
    c.Validation.MinSize(user_email, 5).Message("Email must be greater than 5 characters")
    // Validate Password
    c.Validation.Required(user_password).Message("Password is required")
    c.Validation.MinSize(user_password, 5).Message("Password must be greater than 5 characters.")
    // Validate Password Confirmation
    c.Validation.Required(user_password_confirmation).Message("Password Confirmation is required")
    c.Validation.MinSize(user_password_confirmation, 5).Message("Password must be greater than 5 characters.")
    c.Validation.Required(user_password == user_password_confirmation).Message("Your passwords do not match")
    // Validate First Name
    c.Validation.Required(user_first_name).Message("First Name is required")
    c.Validation.MinSize(user_first_name, 3).Message("Your First Name must be greater than 3 characters")
    // Validate Last Name
    c.Validation.Required(user_last_name).Message("Last Name is required")
    c.Validation.MinSize(user_last_name, 3).Message("Your Last Name must be greater than 3 characters")

    // If anything wasn't right, set flash and display errors to user
    if c.Validation.HasErrors() {
        c.Validation.Keep()
        c.FlashParams()
        return c.Redirect(User.Register)
    }

    // Hash The Users Password
    password := []byte(user_password_confirmation)
    hashedPassword, err := bcrypt.GenerateFromPassword(password, 10)
    if err != nil {
        c.Flash.Error("Invalid Username or Password Combination")
        return c.Redirect(User.Register)
    }

    // Connect to database
    db, err := sql.Open("mysql", "root:******@/WebRTC")
    if err != nil {
        c.Flash.Error("Unable to connect to database. Try again later!")
        return c.Redirect(User.Register)
    }
    defer db.Close()

    // Ping the database, ensure there is a connection
    err = db.Ping()
    if err != nil {
        c.Flash.Error("Unable to connect to database. Try again later!")
        return c.Redirect(User.Register)
    }

    // Prepare SQL Statement for Security
    stmtIns, err := db.Prepare("INSERT INTO users (user_email, user_first_name, user_last_name, " +
        "user_password_hash, user_created_at, user_created_ip, user_last_ip) VALUES (?, ?, ?, ?, ?, ?, ?)")

    if err != nil {
        c.Flash.Error("Unable to Register you. Please try again later")
        return c.Redirect(User.Register)
    }
    defer stmtIns.Close()

    // Insert Data into Database
    _, err = stmtIns.Exec(user_email, user_first_name, user_last_name, hashedPassword,
        time.Now().Local(), c.Request.RemoteAddr, c.Request.RemoteAddr)
    if err != nil {
        c.Flash.Error("Unable to register you. Please try again later")
        return c.Redirect(User.Register)
    }

    // Here I want to add the global template variable
    return c.Redirect(User.Register)
}

我读过关于c.RenderArgs的内容,但似乎并没有按照我的意愿行事。

我希望能够设置用户的用户名,以便我可以在导航栏中显示它,以便他们知道他们已登录。

1 个答案:

答案 0 :(得分:1)

如果你使用的是c.RenderArgs,那么你就是在正确的轨道上,除非我误解了你的意图。

以下是您可以如何使用它的示例(或更确切地说,演练)(取自Revel的booking sample app):

  1. init.go:注册interceptor以在执行操作之前添加用户的信息(AddUser将在Render之前触发)。

    < / LI>
  2. app.go:检查用户是否已连接,如果是,请将用户的数据存储在RenderArgs地图中,如下所示:

  3. c.RenderArgs["user"] = user

    1. 第31行处理model

    2. header.html:在。

    3. 中传递用户名模板变量

      我希望这会有所帮助。

      编辑:我应该在登录时添加用户的用户名为stored in a session。它习惯于retrieve more information