gin

package module
v0.0.0-...-3cf0c3c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 9 Imported by: 0

README

模板结构

./templates
└── default
    ├── errors
    ├── layouts
    │   ├── layout.tmpl
    │   └── pjax_layout.tmpl
    └── pages
        ├── posts
        │   ├── posts.tmpl
        └── posts_detail
            └── posts_detail.tmpl

安装插件

go get -u github.com/gin-contrib/multitemplate

go get -u github.com/nilorg/pkg/gin

加载模板

// 主题名称
const themeName string = "default"

func loadTemplates(templatesDir string) multitemplate.Render {
	r := multitemplate.New()
	// 加载布局
	layouts, err := filepath.Glob(filepath.Join(templatesDir, themeName, "layouts/*.tmpl"))
	if err != nil {
		panic(err.Error())
	}
	// 加载错误页面
	errors, err := filepath.Glob(filepath.Join(templatesDir, themeName, "errors/*.tmpl"))
	if err != nil {
		panic(err.Error())
	}
	for _, errPage := range errors {
		tmplName := fmt.Sprintf("error_%s", filepath.Base(errPage))
		r.AddFromFiles(tmplName, errPage)
	}
	// 页面文件夹
	pages, err := ioutil.ReadDir(filepath.Join(templatesDir, themeName, "pages"))
	if err != nil {
		panic(err.Error())
	}
	for _, page := range pages {
		if !page.IsDir() {
			continue
		}
		for _, layout := range layouts {
			pageItems, err := filepath.Glob(filepath.Join(templatesDir, themeName, fmt.Sprintf("pages/%s/*.tmpl", page.Name())))
			if err != nil {
				panic(err.Error())
			}
			files := []string{
				layout,
			}
			files = append(files,pageItems...)
			tmplName := fmt.Sprintf("%s_pages_%s", filepath.Base(layout), page.Name())
			fmt.Println(tmplName, files)
			r.AddFromFiles(tmplName, files...)
		}
	}
	// 加载单页面
	singles, err := filepath.Glob(filepath.Join(templatesDir, themeName, "singles/*.tmpl"))
	if err != nil {
		panic(err)
	}
	for _, singlePage := range singles {
		tmplName := fmt.Sprintf("singles_%s", filepath.Base(singlePage))
		r.AddFromFiles(tmplName, singlePage)
	}
	return r
}

使用模板

engine := gin.Default()
engine.HTMLRender = loadTemplates("./templates")

给路由配置模板

import ngin "github.com/nilorg/pkg/gin"

engine.GET("/detail", ngin.WebControllerFunc(func(ctx *ngin.WebContext) {
		ctx.RenderPage(gin.H{
			"title":"标题",
		})
	}, "posts_detail"))

Documentation

Index

Constants

View Source
const CurrentAccount = "current_account"

CurrentAccount ...

Variables

This section is empty.

Functions

func WebAPIControllerFunc

func WebAPIControllerFunc(ctlFunc func(ctx *WebAPIContext)) gin.HandlerFunc

WebAPIControllerFunc WebAPI控制器函数

func WebControllerFunc

func WebControllerFunc(ctlFunc func(ctx *WebContext), pageName string) gin.HandlerFunc

WebControllerFunc Web控制器函数

Types

type EngineTemplate

type EngineTemplate struct {
	Errors <-chan error
	// contains filtered or unexported fields
}

EngineTemplate gin引擎模板

func NewEngineTemplate

func NewEngineTemplate(templateDir string, engine *gin.Engine, tmplFunc LoadTemplateFunc) (*EngineTemplate, error)

NewEngineTemplate 创建一个gin引擎模板

func (*EngineTemplate) Close

func (tmpl *EngineTemplate) Close() error

Close 关闭

func (*EngineTemplate) LoadTemplate

func (tmpl *EngineTemplate) LoadTemplate() multitemplate.Render

LoadTemplate 加载模板

func (*EngineTemplate) Watching

func (tmpl *EngineTemplate) Watching() error

Watching 监听模板文件夹中是否有变动

type LoadTemplateFunc

type LoadTemplateFunc func(templatesDir string) multitemplate.Render

LoadTemplateFunc 加载模板函数类

type WebAPIContext

type WebAPIContext struct {
	*gin.Context
}

WebAPIContext Web上下文

func (*WebAPIContext) DelCurrentAccount

func (ctx *WebAPIContext) DelCurrentAccount() error

DelCurrentAccount 删除当前账户

func (*WebAPIContext) GetCurrentAccount

func (ctx *WebAPIContext) GetCurrentAccount() interface{}

GetCurrentAccount 设置当前账户

func (*WebAPIContext) ResultError

func (ctx *WebAPIContext) ResultError(err error)

ResultError 返回错误

func (*WebAPIContext) SetCurrentAccount

func (ctx *WebAPIContext) SetCurrentAccount(data interface{}) error

SetCurrentAccount 设置当前账户

type WebContext

type WebContext struct {
	*gin.Context
	// contains filtered or unexported fields
}

WebContext Web上下文

func (*WebContext) DelCurrentAccount

func (ctx *WebContext) DelCurrentAccount() error

DelCurrentAccount 删除当前账户

func (*WebContext) GetCurrentAccount

func (ctx *WebContext) GetCurrentAccount() interface{}

GetCurrentAccount 设置当前账户

func (*WebContext) RenderPage

func (ctx *WebContext) RenderPage(data gin.H)

RenderPage 渲染页面

func (*WebContext) RenderSinglePage

func (ctx *WebContext) RenderSinglePage(data gin.H)

RenderSinglePage 渲染单页面

func (*WebContext) SetCurrentAccount

func (ctx *WebContext) SetCurrentAccount(data interface{}) error

SetCurrentAccount 设置当前账户

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL