猿问

无法从模板操作 DOM 以添加/删除属性

在我的代码中,我想根据用户身份验证级别禁用一些输入字段,我可以在 JavaScript 中做到这一点,但这是不推荐的解决方案,我想从服务器端做到这一点。


以下是我的go代码;


package main


import (

    "context"

    "html/template"

    "net/http"

    "strings"

    "time"

)


type User struct {

    Flags string

    Title string

}


type UsersPageData struct {

    PageTitle string

    Users     []User

}


func requestTime(next http.Handler) http.Handler {

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

        ctx := r.Context()

        ctx = context.WithValue(ctx, "requestTime", time.Now().Format(time.RFC3339))

        r = r.WithContext(ctx)

        next.ServeHTTP(w, r)

    })

}


func helloHandler(name string) http.Handler {

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

        var title string

        if requestTime := r.Context().Value("requestTime"); requestTime != nil {

            if str, ok := requestTime.(string); ok {

                title = "\ngenerated at: " + str

            }

        }


        master := strings.Join([]string{"admin", "user", "superuser", "read"}, " ")

        admin := strings.Join([]string{"admin"}, " ")

        user := strings.Join([]string{"user"}, " ")


        tmpl := template.Must(template.ParseFiles("index.html"))

        data := UsersPageData{

            PageTitle: "Users list: " + title,

            Users: []User{

                {Flags: master, Title: "Everything"},

                {Flags: admin, Title: "Administrator"},

                {Flags: user, Title: "Normal user"},

            },

        }

        tmpl.Execute(w, data)

    })

}


func main() {

    http.Handle("/john", requestTime(helloHandler("John")))

    http.ListenAndServe(":8080", nil)

}

这是我的模板,包括 JS 代码;


<style>

.admin {

    color: green;

}


.user {

    color: red;

    --btn-disable: 0;

}



[data-authorized="no"] {

  /* Attribute has this exact value */

        cursor: not-allowed;

        pointer-events: none;


        /*Button disabled - CSS color class*/

        color: #c0c0c0;

        background-color: rgb(229, 229, 229) !important;

}



慕姐4208626
浏览 91回答 1
1回答

ibeautiful

在完整代码下方,借助模板自定义函数,我能够做到这一点:package mainimport (&nbsp; &nbsp; "html/template"&nbsp; &nbsp; "net/http")type User struct {&nbsp; &nbsp; Flags []flag //string&nbsp; &nbsp; Title string}type UsersPageData struct {&nbsp; &nbsp; PageTitle string&nbsp; &nbsp; Users&nbsp; &nbsp; &nbsp;[]User}type flag intconst (&nbsp; &nbsp; Admin flag = iota + 1 // iota = 0&nbsp; &nbsp; Editer&nbsp; &nbsp; Superuser&nbsp; &nbsp; Viewer&nbsp; &nbsp; Dummy)func subtract(arg1, arg2 int) int {&nbsp; &nbsp; return arg1 - arg2}func helloHandler(name string) http.Handler {&nbsp; &nbsp; return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; // Map the `subtract` custom function in the template&nbsp; &nbsp; &nbsp; &nbsp; funcMap := map[string]interface{}{"subtract": subtract}&nbsp; &nbsp; &nbsp; &nbsp; master := []flag{Admin, Editer, Superuser, Viewer}&nbsp; &nbsp; &nbsp; &nbsp; admin := []flag{Admin, Superuser, Viewer}&nbsp; &nbsp; &nbsp; &nbsp; user := []flag{Viewer, Dummy}&nbsp; &nbsp; &nbsp; &nbsp; tmpl := template.New("").Funcs(template.FuncMap(funcMap))&nbsp; &nbsp; &nbsp; &nbsp; template.Must(tmpl.ParseFiles("index.html"))&nbsp; &nbsp; &nbsp; &nbsp; data := UsersPageData{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PageTitle: "Users list: ",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Users: []User{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Flags: master, Title: "Everything"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Flags: admin, Title: "Administrator"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Flags: user, Title: "Normal user"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; tmpl.ExecuteTemplate(w, "index.html", data)&nbsp; &nbsp; })}func main() {&nbsp; &nbsp; fs := http.StripPrefix("/www/", http.FileServer(http.Dir("./www")))&nbsp; &nbsp; http.Handle("/www/", fs)&nbsp; &nbsp; http.Handle("/", helloHandler("John"))&nbsp; &nbsp; http.ListenAndServe(":8080", nil)}是index.html:<html>{{/* This is a comment&nbsp;{{$flags := []flag{Admin, Editer, Superuser, Viewer};}}&nbsp;&nbsp; &nbsp; Admin Flag = iota + 1 // iota = 0&nbsp; &nbsp; Editer&nbsp; &nbsp; Superuser&nbsp; &nbsp; Viewer&nbsp; &nbsp; }}*/}}<ul>&nbsp; &nbsp; {{range .Users}}&nbsp; &nbsp; &nbsp; &nbsp; <span>{{.Title}}</span>&nbsp; &nbsp; &nbsp; &nbsp; {{ $done := false}} {{$length := len .Flags}}&nbsp; &nbsp; &nbsp; &nbsp; {{range $i, $v := .Flags}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ if $done }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ else }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{if or (eq $v 1) (eq $v 3)}}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="subject" placeholder= {{$v}} required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ $done = true }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{else}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ if eq $i (subtract $length 1)}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="subject" placeholder= {{$v}} disabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ end }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{end}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{end}}&nbsp; &nbsp; &nbsp; &nbsp; {{end}}&nbsp; &nbsp; {{end}}</ul></html>
随时随地看视频慕课网APP

相关分类

Go
我要回答