ngin

package module
v0.0.0-...-b6d7c90 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: MIT Imports: 11 Imported by: 0

README

ngin

Web framework 包装一些gin功能。

模板结构

./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/ngin

加载模板

// 主题名称
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/ngin"

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

Documentation

Index

Constants

View Source
const SessionCurrentAccount = "current_account"

SessionCurrentAccount ...

Variables

This section is empty.

Functions

func DefaultLoadTemplate

func DefaultLoadTemplate(templatesDir string, funcMap template.FuncMap, templates ...string) multitemplate.Render

DefaultLoadTemplate ...

func NewWebAPIControllerFunc

func NewWebAPIControllerFunc(ctlFunc func(ctx *WebAPIContext), opts ...Option) gin.HandlerFunc

NewWebAPIControllerFunc WebAPI控制器函数

func NewWebControllerFunc

func NewWebControllerFunc(ctlFunc func(ctx *WebContext), opts ...Option) gin.HandlerFunc

NewWebControllerFunc 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, funcMap template.FuncMap, templates ...string) (*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, funcMap template.FuncMap, templates ...string) multitemplate.Render

LoadTemplateFunc 加载模板函数类

type Option

type Option func(*Options)

Option 为可选参数赋值的函数

func GlobalConstant

func GlobalConstant(constant map[string]interface{}) Option

GlobalConstant ...

func GlobalVariable

func GlobalVariable(variable map[string]interface{}) Option

GlobalVariable ...

func Layout

func Layout(layout string) Option

Layout ...

func PageName

func PageName(pageName string) Option

PageName ...

func Pjax

func Pjax(pjax bool) Option

Pjax ...

func PjaxLayout

func PjaxLayout(pjaxLayout string) Option

PjaxLayout ...

func StatusCode

func StatusCode(statusCode int) Option

StatusCode ...

func Template

func Template(template string) Option

Template ...

type Options

type Options struct {
	StatusCode               int
	Layout                   string
	PageName                 string
	Template                 string
	Pjax                     bool
	PjaxLayout               string
	GlobalVariable           map[string]interface{}
	GlobalConstant           map[string]interface{}
	SessionCurrentAccountKey string
}

Options 可选参数列表

type WebAPIContext

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

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, opts ...Option)

RenderPage 渲染页面

func (*WebContext) RenderSinglePage

func (ctx *WebContext) RenderSinglePage(data gin.H, opts ...Option)

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