本文整理汇总了Golang中html/template.CSS函数的典型用法代码示例。如果您正苦于以下问题:Golang CSS函数的具体用法?Golang CSS怎么用?Golang CSS使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CSS函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RenderTemplate
func RenderTemplate(path string, input []byte, metadata map[string]interface{}) []byte {
R := func(relativeFilename string) string {
filename := filepath.Join(filepath.Dir(path), relativeFilename)
return string(RenderTemplate(filename, Read(filename), metadata))
}
importhtml := func(relativeFilename string) template.HTML {
return template.HTML(R(relativeFilename))
}
importcss := func(relativeFilename string) template.CSS {
return template.CSS(R(relativeFilename))
}
importjs := func(relativeFilename string) template.JS {
return template.JS(R(relativeFilename))
}
templateName := Relative(*sourceDir, path)
funcMap := template.FuncMap{
"importhtml": importhtml,
"importcss": importcss,
"importjs": importjs,
"sorted": SortedValues,
}
tmpl, err := template.New(templateName).Funcs(funcMap).Parse(string(input))
if err != nil {
Fatalf("Render Template %s: Parse: %s", path, err)
}
output := bytes.Buffer{}
if err = tmpl.Execute(&output, metadata); err != nil {
Fatalf("Render Template %s: Execute: %s", path, err)
}
return output.Bytes()
}
示例2: init
func init() {
HtmlFuncBoot.Register(func(w *Web) {
// HTML Marksafe
w.HtmlFunc["html"] = func(str string) html.HTML {
return html.HTML(str)
}
// HTML Attr MarkSafe
w.HtmlFunc["htmlattr"] = func(str string) html.HTMLAttr {
return html.HTMLAttr(str)
}
// JS Marksafe
w.HtmlFunc["js"] = func(str string) html.JS {
return html.JS(str)
}
// JS String Marksafe
w.HtmlFunc["jsstr"] = func(str string) html.JSStr {
return html.JSStr(str)
}
// CSS Marksafe
w.HtmlFunc["css"] = func(str string) html.CSS {
return html.CSS(str)
}
})
}
示例3: colors
// colors generates the CSS rules for coverage colors.
func colors() template.CSS {
var buf bytes.Buffer
for i := 0; i < 11; i++ {
fmt.Fprintf(&buf, ".cov%v { color: %v }\n", i, rgb(i))
}
return template.CSS(buf.String())
}
示例4: renderArticle
func renderArticle(c interface{}, path string, arg string) {
// inject content
stylePath := resolveFilename("style.css")
coverPath := resolveFilename("_cover")
styleContent, _ := ioutil.ReadFile(stylePath)
coverJSON, _ := ioutil.ReadFile(coverPath)
article := readArticle(path)
data := gin.H{
"title": filepath.Base(path),
"article": template.JS(article.data),
"useMathjax": article.useMathjax,
"style": template.CSS(styleContent),
"cover": template.JS(coverJSON),
}
switch target := c.(type) {
case *gin.Context:
// arg is the template used
target.HTML(http.StatusOK, arg, data)
case *template.Template:
// arg is the file to be written to
w, err := createFile(arg)
if err != nil {
panic(err.Error())
}
target.Execute(w, data)
}
}
示例5: ForegroundColor
// ForegroundColor calculates the text color for labels based
// on their background color.
func (l *Label) ForegroundColor() template.CSS {
if strings.HasPrefix(l.Color, "#") {
if color, err := strconv.ParseUint(l.Color[1:], 16, 64); err == nil {
r := float32(0xFF & (color >> 16))
g := float32(0xFF & (color >> 8))
b := float32(0xFF & color)
luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255
if luminance < 0.5 {
return template.CSS("#fff")
}
}
}
// default to black
return template.CSS("#000")
}
示例6: colorNick
func colorNick(s string) template.CSS {
hue := uint(0)
for _, c := range s {
hue *= 17
hue += uint(c)
}
style := fmt.Sprintf("color: hsl(%d, 40%%, 50%%)", hue%360)
return template.CSS(style)
}
示例7: TemplateHandler
// An example http.Handler that serves a template
func TemplateHandler(w http.ResponseWriter, r *http.Request) {
// The JS() and CSS() functions will safely escape the given string
// according to the requested format.
attrs := map[string]interface{}{
"Title": "I am a Title from Attributes",
"JS": template.JS("console.log('I am safely injected JavaScript!');"),
"CSS": template.CSS("p { color: blue; border: 1px solid #aaa }"),
}
example.Execute(w, attrs)
}
示例8: CSS
func (c *Chart) CSS() template.CSS {
h, w := c.Height, c.Width
if h == 0 {
h = 150
}
if w == 0 {
w = 300
}
return template.CSS(fmt.Sprintf("width:%dpx;height:%dpx", w, h))
}
示例9: newInvitationEmailHTMLContent
func newInvitationEmailHTMLContent(title, inlineStyles, name, company, invitedBy, link string) (string, error) {
// Layout
layoutContents, err := ioutil.ReadFile(htmlEmailLayout)
if err != nil {
return "", err
}
layoutTmpl, err := htmlTemplate.New(htmlEmailLayout).Parse(string(layoutContents))
if err != nil {
return "", err
}
// Content
templateContents, err := ioutil.ReadFile(invitationEmailTemplateHTML)
if err != nil {
return "", err
}
tmpl, err := htmlTemplate.New(invitationEmailTemplateHTML).Parse(string(templateContents))
if err != nil {
return "", err
}
var (
inventory map[string]interface{}
parsedContent, parsedLayout bytes.Buffer
)
// Parse the content template
inventory = map[string]interface{}{
"name": name,
"company": company,
"invitedBy": invitedBy,
"invitationURL": link,
}
if err := tmpl.Execute(&parsedContent, inventory); err != nil {
return "", err
}
// Insert the content into the layout
inventory = map[string]interface{}{
"title": title,
"inlineStyles": htmlTemplate.CSS(inlineStyles),
"content": htmlTemplate.HTML(parsedContent.String()),
"company": company,
}
if err := layoutTmpl.Execute(&parsedLayout, inventory); err != nil {
return "", err
}
return parsedLayout.String(), nil
}
示例10: init
func init() {
//初始化基本模板函数
//until(start, end int) []int:生成一个从start到end的数组
//time(t time.Time) string:返回时间字符串2006-01-02 15:04:05
//date(t time.Time) string:返回日期字符串2006-01-02
//tocss(s string) template.CSS:转换字符串为CSS
//tohtml(s string) template.HTML:转换字符串为HTML
//toattr(s string) template.HTMLAttr:转换字符串为HTMLAttr
//tojs(s string) template.JS:转换字符串为JS
//tojsstr(s string) template.JSStr:转换字符串为JSStr
//tourl(s string) template.URL:转换字符串为URL
commonFuncMap = template.FuncMap{
"until": func(start, end int) []int {
if end >= start {
var length = end - start + 1
var result = make([]int, length)
for i := 0; i < length; i++ {
result[i] = start + i
}
return result
}
return []int{}
},
"time": func(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
},
"date": func(t time.Time) string {
return t.Format("2006-01-02")
},
"tocss": func(s string) template.CSS {
return template.CSS(s)
},
"tohtml": func(s string) template.HTML {
return template.HTML(s)
},
"toattr": func(s string) template.HTMLAttr {
return template.HTMLAttr(s)
},
"tojs": func(s string) template.JS {
return template.JS(s)
},
"tojsstr": func(s string) template.JSStr {
return template.JSStr(s)
},
"tourl": func(s string) template.URL {
return template.URL(s)
},
}
}
示例11: render
// openDiffs diffs the given files and writes the result to a tempfile,
// then opens it in the gui.
func render(d *delta.DiffSolution, pathFrom, pathTo, pathBase string, config Config) (*bytes.Buffer, error) {
change := changeModified
if pathTo == "/dev/null" {
change = changeDeleted
} else if pathFrom == "/dev/null" {
change = changeAdded
}
// normalize paths so we don't have tmp on the path
tmpFrom := strings.HasPrefix(pathFrom, os.TempDir())
tmpTo := strings.HasPrefix(pathTo, os.TempDir())
if tmpFrom && !tmpTo {
pathFrom = pathTo
} else if !tmpFrom && tmpTo {
pathTo = pathFrom
}
wd, _ := os.Getwd()
html := formatter.HTML(d)
m := &Metadata{
From: pathFrom,
To: pathTo,
Merged: pathBase,
Dir: wd,
Change: change,
Hash: md5sum(html),
DirHash: md5sum(wd),
Timestamp: time.Now().UnixNano() / 1000000, // convert to millis
}
meta, _ := json.Marshal(m)
cfg, _ := json.Marshal(config)
tmpl := template.Must(template.New("compare").Parse(getAsset("compare.html")))
buf := &bytes.Buffer{}
err := tmpl.Execute(buf, map[string]interface{}{
"metadata": template.JS(string(meta)),
"config": template.JS(cfg),
"content": template.HTML(html),
"CSS": template.CSS(getAsset("app.css")),
"JS": map[string]interface{}{
"mithril": template.JS(getAsset("vendor/mithril.min.js")),
"mousetrap": template.JS(getAsset("vendor/mousetrap.min.js")),
"highlight": template.JS(getAsset("vendor/highlight.min.js")),
"pouchdb": template.JS(getAsset("vendor/pouchdb.min.js")),
"app": template.JS(getAsset("app.js")),
},
})
return buf, err
}
示例12: TestToString
func TestToString(t *testing.T) {
var foo interface{} = "one more time"
assert.Equal(t, ToString(8), "8")
assert.Equal(t, ToString(int64(16)), "16")
assert.Equal(t, ToString(8.12), "8.12")
assert.Equal(t, ToString([]byte("one time")), "one time")
assert.Equal(t, ToString(template.HTML("one time")), "one time")
assert.Equal(t, ToString(template.URL("http://somehost.foo")), "http://somehost.foo")
assert.Equal(t, ToString(template.JS("(1+2)")), "(1+2)")
assert.Equal(t, ToString(template.CSS("a")), "a")
assert.Equal(t, ToString(template.HTMLAttr("a")), "a")
assert.Equal(t, ToString(foo), "one more time")
assert.Equal(t, ToString(nil), "")
assert.Equal(t, ToString(true), "true")
assert.Equal(t, ToString(false), "false")
}
示例13: Init
// Must be called after flag.Parse()
func Init(tlsCertPath, tlsKeyPath, staticPath string) {
loadTemplates()
b, err := ioutil.ReadFile(filepath.Join(staticPath, "critical.min.css"))
if err != nil {
log.Fatal(err)
}
CriticalCss = template.CSS(string(b))
addrs := strings.Split(*sourceBackends, ",")
SourceBackendStubs = make([]proto.SourceBackendClient, len(addrs))
for idx, addr := range addrs {
conn, err := grpcutil.DialTLS(addr, tlsCertPath, tlsKeyPath)
if err != nil {
log.Fatalf("could not connect to %q: %v", addr, err)
}
SourceBackendStubs[idx] = proto.NewSourceBackendClient(conn)
}
}
示例14: Init
func (m middlewareView) Init(app *App) error {
app.requestMiddlewares = append(app.requestMiddlewares, requestMiddlewareView)
templates := template.New("")
templateFuncs := template.FuncMap{}
templateFuncs["Plain"] = func(data string) template.HTML {
return template.HTML(data)
}
templateFuncs["CSS"] = func(data string) template.CSS {
return template.CSS(data)
}
templates = templates.Funcs(templateFuncs)
app.data["templates"] = templates
app.data["templateFuncs"] = templateFuncs
return nil
}
示例15: AddFile
func (doc *Content) AddFile(f string) error {
switch path.Ext(f) {
case ".js":
data, err := ioutil.ReadFile(f)
if err != nil {
return err
}
doc.JS = append(doc.JS, template.JS(data))
case ".css":
data, err := ioutil.ReadFile(f)
if err != nil {
return err
}
doc.CSS = append(doc.CSS, template.CSS(data))
case ".html":
data, err := ioutil.ReadFile(f)
if err != nil {
return err
}
doc.HTML = append(doc.HTML, template.HTML(data))
}
return nil
}