Components
Component development, taking the image component as an example.

New types and methods of upper interface

    Create a new ImgAttribute type
1
type ImgAttribute interface {
2
SetWidth(value string) ImgAttribute
3
SetHeight(value string) ImgAttribute
4
SetSrc(value string) ImgAttribute
5
GetContent() template.HTML
6
}
Copied!
    In the Template interface, add a method:
1
type Template interface {
2
...
3
Image() types.ImgAttribute
4
...
5
}
Copied!

Specific implementation, with adminlte as an example

    ImgAttribute
Create a new image.go file under ./template/adminlte/components, as follows:
1
package components
2
3
import (
4
"github.com/GoAdminGroup/go-admin/template/types"
5
"html/template"
6
)
7
8
type ImgAttribute struct {
9
Name string
10
Witdh string
11
Height string
12
Src string
13
}
14
15
func (compo *ImgAttribute) SetWidth(value string) types.ImgAttribute {
16
compo.Witdh = value
17
return compo
18
}
19
20
func (compo *ImgAttribute) SetHeight(value string) types.ImgAttribute {
21
compo.Height = value
22
return compo
23
}
24
25
func (compo *ImgAttribute) SetSrc(value string) types.ImgAttribute {
26
compo.Src = value
27
return compo
28
}
29
30
func (compo *ImgAttribute) GetContent() template.HTML {
31
return ComposeHtml(compo.TemplateList, *compo, "image")
32
}
Copied!
    Image()
In .template/adminlte/adminlte.go, add a function:
1
func (*Theme) Image() types.ImgAttribute {
2
return &components.ImgAttribute{
3
Name: "image",
4
Witdh: "50",
5
Height: "50",
6
Src: "",
7
}
8
}
Copied!
Still not completed here, you need to add static resource files.
    Add the static resource file
Add image.tmpl file to .template/adminlte/resource/pages/components
Annoying, and the last step
    Execute in the root directory:
1
adm assets
Copied!
Last modified 1yr ago