diff --git a/sui/api/request.go b/sui/api/request.go index 64880a2b..d10147c5 100644 --- a/sui/api/request.go +++ b/sui/api/request.go @@ -164,9 +164,9 @@ func (r *Request) Render() (string, int, error) { return "", code, err } - data := core.Data{} + data := r.Request.NewData() if c.Data != "" { - data, err = r.Request.ExecString(c.Data) + err = r.Request.ExecStringMerge(data, c.Data) if err != nil { return "", 500, fmt.Errorf("data error, please re-complie the page %s", err.Error()) } @@ -181,10 +181,9 @@ func (r *Request) Render() (string, int, error) { } // Set the page request data - r.Request.WithData(data) option := core.ParserOption{ - Theme: data["$theme"].(string), - Lang: data["$lang"].(string), + Theme: r.Request.Theme, + Locale: r.Request.Locale, Debug: r.Request.DebugMode(), Request: true, } diff --git a/sui/core/page_test.go b/sui/core/page_test.go index e8c1dbf5..5f478ff8 100644 --- a/sui/core/page_test.go +++ b/sui/core/page_test.go @@ -17,7 +17,7 @@ func TestPageExec(t *testing.T) { request := &Request{ URL: ReqeustURL{Path: "/test/path"}, Query: map[string][]string{"show": {"yes"}}, - Locale: "zh-CN", + Locale: "zh-cn", Theme: "dark", } diff --git a/sui/core/parser.go b/sui/core/parser.go index 6f06d7a6..bfd1b255 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -29,12 +29,12 @@ type Mapping struct { // ParserOption parser option type ParserOption struct { - Editor bool `json:"editor,omitempty"` - Preview bool `json:"preview,omitempty"` - Debug bool `json:"debug,omitempty"` - Request bool `json:"request,omitempty"` - Theme string `json:"theme,omitempty"` - Lang string `json:"lang,omitempty"` + Editor bool `json:"editor,omitempty"` + Preview bool `json:"preview,omitempty"` + Debug bool `json:"debug,omitempty"` + Request bool `json:"request,omitempty"` + Theme any `json:"theme,omitempty"` + Locale any `json:"locale,omitempty"` } var keepWords = map[string]bool{ diff --git a/sui/core/request.go b/sui/core/request.go index 208c8852..5e114217 100644 --- a/sui/core/request.go +++ b/sui/core/request.go @@ -72,33 +72,54 @@ func (r *Request) DisableCache() bool { return disable } -// WithData set the data -func (r *Request) WithData(data Data) *Request { +// NewData create the new data +func (r *Request) NewData() Data { cookies := r.Cookies() + theme := GetTheme(cookies) + locale := GetLocale(cookies) + r.Theme = theme + r.Locale = locale + + data := Data{} data["$payload"] = r.Payload data["$query"] = r.Query data["$param"] = r.Params data["$cookie"] = cookies data["$url"] = r.URL - data["$theme"] = GetTheme(cookies) - data["$lang"] = GetLang(cookies) - return r + data["$theme"] = r.Theme + data["$locale"] = r.Locale + return data } -// GetLang get the lang -func GetLang(cookies map[string]string) string { - if lang, has := cookies["lang"]; has { +// GetLocale get the locale +func GetLocale(cookies map[string]string) interface{} { + if lang, has := cookies["locale"]; has { return lang } - return "" + return nil } // GetTheme get the theme -func GetTheme(cookies map[string]string) string { +func GetTheme(cookies map[string]string) interface{} { if theme, has := cookies["color-theme"]; has { return theme } - return "" + return nil +} + +// ExecStringMerge exec the string and merge the data +func (r *Request) ExecStringMerge(data Data, raw string) error { + + res, err := r.ExecString(raw) + if err != nil { + return err + } + + // Merge the data + for key, value := range res { + data[key] = value + } + return nil } // ExecString get the data diff --git a/sui/core/types.go b/sui/core/types.go index 4744e1f6..e8537b5b 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -160,8 +160,8 @@ type Request struct { Body interface{} `json:"body,omitempty"` URL ReqeustURL `json:"url,omitempty"` Sid string `json:"sid,omitempty"` - Theme string `json:"theme,omitempty"` - Locale string `json:"locale,omitempty"` + Theme any `json:"theme,omitempty"` + Locale any `json:"locale,omitempty"` } // RequestSource is the struct for the request @@ -307,21 +307,21 @@ type Matcher struct { // DocumentDefault is the default document var DocumentDefault = []byte(` - +
-