mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 19:57:40 +08:00
add package gmode for release mode management; add error returning for template rendoring for gmvc.View/ghttp.Response
This commit is contained in:
parent
565e48f6f1
commit
1d264a6545
@ -9,6 +9,8 @@ package gmvc
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/gogf/gf/util/gmode"
|
||||
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
)
|
||||
@ -91,22 +93,28 @@ func (view *View) BindFuncMap(funcMap gview.FuncMap) {
|
||||
}
|
||||
|
||||
// 解析并显示指定模板
|
||||
func (view *View) Display(file ...string) {
|
||||
func (view *View) Display(file ...string) error {
|
||||
name := "index.tpl"
|
||||
if len(file) > 0 {
|
||||
name = file[0]
|
||||
}
|
||||
if content, err := view.Parse(name); err != nil {
|
||||
view.response.Write("Tpl Parsing Error: " + err.Error())
|
||||
} else {
|
||||
view.response.Write(content)
|
||||
}
|
||||
}
|
||||
|
||||
// 解析并显示模板内容
|
||||
func (view *View) DisplayContent(content string) error {
|
||||
if content, err := view.ParseContent(content); err != nil {
|
||||
view.response.Write("Tpl Parsing Error: " + err.Error())
|
||||
if gmode.IsDevelop() {
|
||||
view.response.Write("Tpl Parsing Error: " + err.Error())
|
||||
}
|
||||
return err
|
||||
} else {
|
||||
view.response.Write(content)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 解析并显示模板内容
|
||||
func (view *View) DisplayContent(content string) error {
|
||||
if content, err := view.ParseContent(content); err != nil {
|
||||
if gmode.IsDevelop() {
|
||||
view.response.Write("Tpl Parsing Error: " + err.Error())
|
||||
}
|
||||
return err
|
||||
} else {
|
||||
view.response.Write(content)
|
||||
|
@ -10,24 +10,33 @@ package ghttp
|
||||
import (
|
||||
"github.com/gogf/gf/frame/gins"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/util/gmode"
|
||||
)
|
||||
|
||||
// 展示模板,可以给定模板参数,及临时的自定义模板函数
|
||||
func (r *Response) WriteTpl(tpl string, params ...gview.Params) {
|
||||
func (r *Response) WriteTpl(tpl string, params ...gview.Params) error {
|
||||
if b, err := r.ParseTpl(tpl, params...); err != nil {
|
||||
r.Write("Template Parsing Error: " + err.Error())
|
||||
if gmode.IsDevelop() {
|
||||
r.Write("Template Parsing Error: " + err.Error())
|
||||
}
|
||||
return err
|
||||
} else {
|
||||
r.Write(b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 展示模板内容,可以给定模板参数,及临时的自定义模板函数
|
||||
func (r *Response) WriteTplContent(content string, params ...gview.Params) {
|
||||
func (r *Response) WriteTplContent(content string, params ...gview.Params) error {
|
||||
if b, err := r.ParseTplContent(content, params...); err != nil {
|
||||
r.Write("Template Parsing Error: " + err.Error())
|
||||
if gmode.IsDevelop() {
|
||||
r.Write("Template Parsing Error: " + err.Error())
|
||||
}
|
||||
return err
|
||||
} else {
|
||||
r.Write(b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 解析模板文件,并返回模板内容
|
||||
|
80
util/gmode/gmode.go
Normal file
80
util/gmode/gmode.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gmode provides release mode management for project.
|
||||
package gmode
|
||||
|
||||
import "github.com/gogf/gf/os/gfile"
|
||||
|
||||
const (
|
||||
NOT_SET = 0
|
||||
DEVELOP = 1
|
||||
TESTING = 2
|
||||
STAGING = 3
|
||||
PRODUCT = 4
|
||||
)
|
||||
|
||||
var (
|
||||
currentMode = NOT_SET
|
||||
)
|
||||
|
||||
// Set sets the mode for current application.
|
||||
func Set(mode int) {
|
||||
currentMode = mode
|
||||
}
|
||||
|
||||
// SetDevelop sets current mode DEVELOP for current application.
|
||||
func SetDevelop() {
|
||||
Set(DEVELOP)
|
||||
}
|
||||
|
||||
// SetTesting sets current mode TESTING for current application.
|
||||
func SetTesting() {
|
||||
Set(TESTING)
|
||||
}
|
||||
|
||||
// SetStaging sets current mode STAGING for current application.
|
||||
func SetStaging() {
|
||||
Set(STAGING)
|
||||
}
|
||||
|
||||
// SetProduct sets current mode PRODUCT for current application.
|
||||
func SetProduct() {
|
||||
Set(PRODUCT)
|
||||
}
|
||||
|
||||
// Mode returns current application mode set.
|
||||
func Mode() int {
|
||||
// If current mode is not set, do this auto check.
|
||||
if currentMode == NOT_SET {
|
||||
if gfile.MainPkgPath() != "" {
|
||||
return DEVELOP
|
||||
} else {
|
||||
return PRODUCT
|
||||
}
|
||||
}
|
||||
return currentMode
|
||||
}
|
||||
|
||||
// IsDevelop checks and returns whether current application is running in DEVELOP mode.
|
||||
func IsDevelop() bool {
|
||||
return Mode() == DEVELOP
|
||||
}
|
||||
|
||||
// IsTesting checks and returns whether current application is running in TESTING mode.
|
||||
func IsTesting() bool {
|
||||
return Mode() == TESTING
|
||||
}
|
||||
|
||||
// IsStaging checks and returns whether current application is running in STAGING mode.
|
||||
func IsStaging() bool {
|
||||
return Mode() == STAGING
|
||||
}
|
||||
|
||||
// IsProduct checks and returns whether current application is running in PRODUCT mode.
|
||||
func IsProduct() bool {
|
||||
return Mode() == PRODUCT
|
||||
}
|
Loading…
Reference in New Issue
Block a user