/
page.go
45 lines (36 loc) · 1.45 KB
/
page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
package page
import (
"bytes"
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/db"
"github.com/GoAdminGroup/go-admin/modules/logger"
"github.com/GoAdminGroup/go-admin/modules/menu"
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
"github.com/GoAdminGroup/go-admin/template"
"github.com/GoAdminGroup/go-admin/template/types"
)
// SetPageContent set and return the panel of page content.
func SetPageContent(ctx *context.Context, user models.UserModel, c func(ctx interface{}) (types.Panel, error), conn db.Connection) {
panel, err := c(ctx)
if err != nil {
logger.Error("SetPageContent", err)
panel = template.WarningPanel(err.Error())
}
tmpl, tmplName := template.Get(config.GetTheme()).GetTemplate(ctx.IsPjax())
ctx.AddHeader("Content-Type", "text/html; charset=utf-8")
buf := new(bytes.Buffer)
err = tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(types.NewPageParam{
User: user,
Menu: menu.GetGlobalMenu(user, conn).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Panel: panel.GetContent(config.IsProductionEnvironment()),
Assets: template.GetComponentAssetImportHTML(),
}))
if err != nil {
logger.Error("SetPageContent", err)
}
ctx.WriteString(buf.String())
}