FET is a golang template engine that can tranlate smarty like template code into golang html/template.
FET means Friendly, Easily for Template code. The official golang package html/template is a fully functional template engine, but it has a few defects with user experience, so you need FET.
- Expression logics
- Use
inclduewith defined variables scopes - Use
extendsinherit base template with defined variables scopes - Limited support for
forandcapture
it's more likely to the php template engine smarty.
-
inherit
{%extends "base.html"%} -
blocks for inherit
{%block "header"%} <div>some code here</div> {%/block%} -
include
{%include file="header.html" var=1%} -
loop, do not support keyword
breakcontinue// for Gofet mode {%for item,key in list%} // output {%/for%} {%for i = 0, j = 10; i < j; i++%} // output {%/for%} // for Smarty mode {%foreach $list as $key => $item%} // output {%/foreach%} // for {%for $i = 0, $j = 10; $i < $j; $i++%} // output {%/for%}
-
if condition
{%if $num > 100%} {%elseif $num < 50%} {%else%} {%/if%} -
output
{%$item.url%} -
pipe funcs
{%$item.title|truncate:30%} -
variable define
{%$title = "this is a title"%} -
capture
{%capture "hello"%}hello{%/capture%} {%if true%} {%$fet.capture.hello%},wolrd! {%else%} just {%$fet.capture.hello%}! {%/if%} -
static variables
{%$fet.capture.xxx%} {%$fet.config.leftDelimiter%} {%$fet.config.rightDelimiter%} {%$fet.config.templateDir%} {%$fet.config.compileDir%} {%$fet.now%} {%$fet.debug%} // will output all the variables that assigned in the template<include variables that difined in the template by yourself> to the js devtools's Console panel. -
special variables
{%$ROOT%} // will output $ {%$fet%} // will output .
-
operators: You can either use the keyword operator or the punctuation.
keyword punctuation example and&&1 && 2<=>1 and 2or||1 || 2<=>1 or 2not!!a<=>not aeq==a == b<=>a eq bne!=a != b<=>a ne bgt>a > b<=>a gt bge>=a >= b<=>a ge blt<a < b<=>a lt ble<=a <= b<=>a le bbitor- a bitor b- &a & b- ^a ^ b- +a + b- -a - b- *a * b- /a / b- %a % b- **a ** bBe careful of the
andandoroperators, they don't have short circuit with conditions. -
pipe
|pipeline funcs
:set parameters for pipeline funcs -
numbers
hex:0xffff
octal:0o777
binary:0b1000
scientific notation1e10
{% $sayHello = "world" %}
{% "hello `$sayHello`"%} // output "hello world"use `` for variable or expression in strings. do not use +.
-
Math
minmaxfloorceil -
Formats
number_format -
Strings
truncateconcatucwords -
Assert
empty -
Length
count -
Output
safe
- types.Smarty
the variable and field must begin with$, useforeachtag for loops.
- types.Gofet
the variable and field mustn't begin with$, usefortag for loops.
# install command line tool `fetc`
go get -v github.com/fefit/fetc
# then init the config, will make a config file `fet.config.json`
fetc init
# then watch the file change, compile your fet template file immediately
fetc watchpackage main
import (
"os"
"github.com/fefit/fet"
"github.com/fefit/fet/types"
)
func main(){
conf := &fet.Config{
LeftDelimiter: "{%", // default "{%"
RightDelimiter: "%}", // default "%}"
TemplateDir: "tmpls", // default "templates"
CompileDir: "views", // default "templates_c",
Ignores: []string{"inc/*"}, // ignore compile,paths and files that will be included and extended. use filepath.Match() method.
UcaseField: false, // default false, if true will auto uppercase field name to uppercase.
CompileOnline: false, // default false, you should compile your template files offline
Glob: false, // default false, if true, will add {{define "xxx"}}{{end}} to wrap the compiled content,"xxx" is the relative pathname base on your templateDir, without the file extname.
AutoRoot: false, // default false,if true, if the variable is not assign in the scope, will treat it as the root field of template data, otherwise you need use '$ROOT' to index the data field.
Mode: types.Smarty, // default types.Smarty, also can be "types.Gofet"
}
fet, _ := fet.New(conf)
// assign data
data := map[string]interface{}{
"Hello": "World"
}
// the index.html {%$ROOT.Hello%}
// Display
fet.Display("index.html", data, os.Stdout)
// will output: World
}-
fet.LoadConf(configFile string) (*types.FetConfig, error)if you use the command line
fetcbuild a config filefet.config.json, then you can usefet.LoadConfto get the config. -
fet.New(config *types.FetConfig) (instance *Fet, error)get a fet instance.
-
instance.Compile(tpl string, createFile bool) (result string, err error)compile a template file, if
createFileis true, will create the compiled file. -
instance.CompileAll() errorcompile all files need to compile.
-
instance.Display(tpl string, data interface{}, output io.Wirter) errorrender the parsed html code into
output. -
instance.Fetch(tpl string, data interface{}) (result string, err error)just get the parsed
stringcode, it always useCompileOnlinemode.
-
compile modejust use fet compile your template files offline, and add the FuncMap
lib/funcs/funcs.goto your project. -
install modeinstall
fet,and usefet.Display(tpl, data, io.Writer)to render the template file.