Package jade (github /Joker/jade) is a simple and fast template engine implementing Jade/Pug template.
Jade precompiles templates to Go code or generates html/template.
Now Jade-lang is renamed toPug template engine.
example:
//-:go:func Index(pageTitle string, youAreUsingJade bool)
mixinfor(golang)
#cmdPrecompile jade templates to#{golang}code.
doctype html
html(lang="en")
head
title=pageTitle
script(type='text/javascript').
if(question){
answer(40+2)
}
body
h1 Jade - template engine
+for('Go')
#container.col
ifyouAreUsingJade
p You are amazing
else
p Get on it!
p.
Jade/Pug is a terse and simple
templating language with
a#[strong focus]on performance
and powerful features.
becomes
<!DOCTYPE html>
<htmllang= "en">
<head>
<title>Jade.go</title>
<scripttype= "text/javascript">
if(question){
answer(40+2)
}
</script>
</head>
<body>
<h1>Jade - template engine
<divid= "cmd">Precompile jade templates to Go code.</div>
</h1>
<divid= "container"class= "col">
<p>You are amazing</p>
<p>
Jade/Pug is a terse and simple
templating language with
a<strong>focus</strong>on performance
and powerful features.
</p>
</div>
</body>
</html>
Here are additionalexamplesandtest cases.
Installjade compiler
go install github /Joker/jade/cmd/jade@latest
or github /Joker/jade package
go get -u github /Joker/jade
jade -writer -pkg=main hello.jade
jade command1precompileshello.jadetohello.jade.go
hello.jade
:go:func(arg) word string
doctype 5
html
body
p Hello #{word}!
hello.jade.go
// Code generated by "jade.go"; DO NOT EDIT.
packagemain
import"io"
const(
hello__0=`<!DOCTYPE html><html><body><p>Hello `
hello__1=`!</p></body></html>`
)
funcJade_hello(wordstring,wrio.Writer) {
buffer:=&WriterAsBuffer{wr}
buffer.WriteString(hello__0)
WriteEscString(word,buffer)
buffer.WriteString(hello__1)
}
main.go
packagemain
//go:generate jade -pkg=main -writer hello.jade
import"net/http"
funcmain() {
http.HandleFunc("/",func(wrhttp.ResponseWriter,req*http.Request) {
Jade_hello("jade",wr)
})
http.ListenAndServe(":8080",nil)
}
output at localhost:8080
<!DOCTYPE html><html><body><p>Hello jade!</p></body></html>
generatehtml/template
at runtime
(This case is slightly slower and doesn't support2all features of Jade.go)
packagemain
import(
"fmt"
"html/template"
"net/http"
"github /Joker/hpp"// Prettify HTML
"github /Joker/jade"
)
funchandler(whttp.ResponseWriter,r*http.Request) {
jadeTpl,_:=jade.Parse("jade",[]byte("doctype 5\nhtml: body: p Hello #{.Word}! "))
goTpl,_:=template.New("html").Parse(jadeTpl)
fmt.Printf("output:%s\n\n",hpp.PrPrint(jadeTpl))
goTpl.Execute(w,struct{Wordstring}{"jade"})
}
funcmain() {
http.HandleFunc("/",handler)
http.ListenAndServe(":8080",nil)
}
console output
<!DOCTYPE html>
<html>
<body>
<p>Hello {{.Word}}!</p>
</body>
</html>
output at localhost:8080
<!DOCTYPE html><html><body><p>Hello jade!</p></body></html>
The data of chart comes fromSlinSo/goTemplateBenchmark.
This filter is used as helper for command line tool
(to set imports, function name and parameters).
Filter may be placed at any nesting level.
When Jade used as library:go filter is not needed.
:go:func
CustomNameForTemplateFunc(any []int, input string, args map[string]int)
:go:func(name)
OnlyCustomNameForTemplateFunc
:go:func(args)
(only string, input float32, args uint)
:go:import
"github /Joker/jade"
github /Joker/hpp
Footnotes
-
Usage:./jade [OPTION]... [FILE]...
↩-basedir string base directory for templates (default "./" ) -d string directory for generated.go files (default "./" ) -fmt HTML pretty print output for generated functions -inline inline HTML in generated functions -pkg string package name for generated files (default "jade" ) -stdbuf use bytes.Buffer [default bytebufferpool.ByteBuffer] -stdlib use stdlib functions -writer use io.Writer for output
-
Runtime
html/template
generation doesn't support the following features:
=>
means it generate the folowing templatefor => "{{/* %s, %s */}}{{ range %s }}" for if => "{{ if gt len %s 0 }}{{/* %s, %s */}}{{ range %s }}" multiline code => "{{/* %s */}}" inheritance block => "{{/* block */}}" case statement => "{{/* switch %s */}}" when => "{{/* case %s: */}}" default => "{{/* default: */}}"
You can change this behaviour in
config.go
file.
Partly this problem can be solved bycustomfunctions.↩