github.com/kotovmak/go-admin@v1.1.1/template/installation/installation.go (about)

     1  package login
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"html/template"
     7  	"strings"
     8  
     9  	"github.com/kotovmak/go-admin/modules/language"
    10  	"github.com/kotovmak/go-admin/modules/logger"
    11  )
    12  
    13  type Installation struct {
    14  	Name string
    15  }
    16  
    17  func Get() *Installation {
    18  	return &Installation{
    19  		Name: "installation",
    20  	}
    21  }
    22  
    23  var DefaultFuncMap = template.FuncMap{
    24  	"lang":     language.Get,
    25  	"langHtml": language.GetFromHtml,
    26  	"link": func(cdnUrl, prefixUrl, assetsUrl string) string {
    27  		if cdnUrl == "" {
    28  			return prefixUrl + assetsUrl
    29  		}
    30  		return cdnUrl + assetsUrl
    31  	},
    32  	"isLinkUrl": func(s string) bool {
    33  		return (len(s) > 7 && s[:7] == "http://") || (len(s) > 8 && s[:8] == "https://")
    34  	},
    35  	"render": func(s, old, repl template.HTML) template.HTML {
    36  		return template.HTML(strings.ReplaceAll(string(s), string(old), string(repl)))
    37  	},
    38  	"renderJS": func(s template.JS, old, repl template.HTML) template.JS {
    39  		return template.JS(strings.ReplaceAll(string(s), string(old), string(repl)))
    40  	},
    41  	"divide": func(a, b int) int {
    42  		return a / b
    43  	},
    44  }
    45  
    46  func (i *Installation) GetTemplate() (*template.Template, string) {
    47  	tmpl, err := template.New("installation").
    48  		Funcs(DefaultFuncMap).
    49  		Parse(List["installation"])
    50  
    51  	if err != nil {
    52  		logger.Error("Installation GetTemplate Error: ", err)
    53  	}
    54  
    55  	return tmpl, "installation"
    56  }
    57  
    58  func (i *Installation) GetAssetList() []string               { return AssetsList }
    59  func (i *Installation) GetAsset(name string) ([]byte, error) { return Asset(name[1:]) }
    60  func (i *Installation) IsAPage() bool                        { return true }
    61  func (i *Installation) GetName() string                      { return "login" }
    62  
    63  func (i *Installation) GetContent() template.HTML {
    64  	buffer := new(bytes.Buffer)
    65  	tmpl, defineName := i.GetTemplate()
    66  	err := tmpl.ExecuteTemplate(buffer, defineName, i)
    67  	if err != nil {
    68  		fmt.Println("ComposeHtml Error:", err)
    69  	}
    70  	return template.HTML(buffer.String())
    71  }