Skip to content

Commit

Permalink
add $fet.debug, fix json_encode
Browse files Browse the repository at this point in the history
  • Loading branch information
ganmin committed Jun 5, 2020
1 parent 99b4c01 commit a72ff73
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 154 deletions.
190 changes: 97 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# FET
FET is a go template engineer that can translate code to `html/template` code.

FET is a go template engineer that can translate code to `html/template` code.

## Why FET

FET means Friendly, Easily for Templating.`html/template` has a basic support for templating, but it's not easy to use, so you need FET.

- Expression support
- Use `incldue` with defined variables scopes
- Use `extends` inherit base template with defined variables scopes
- Extends support for `for` loop, e.g

## Document
[Document](https://github.com/fefit/fet/wiki/Wiki)

[Document](https://github.com/fefit/fet/wiki/Wiki)

[中文文档](https://github.com/fefit/fet/wiki/%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3)

Expand All @@ -26,7 +28,7 @@ it's more like the php template engineer smarty.
```

- blocks for inherit

```php
{%block "header"%}
<div>some code here</div>
Expand All @@ -40,13 +42,13 @@ it's more like the php template engineer smarty.
```

- loop, do not support keyword `break` `continue`

```php
// for Gofet mode
{%for item,key in list%}
// output
{%/for%}

{%for i = 0, j = 10; i < j; i++%}
// output
{%/for%}
Expand All @@ -61,19 +63,19 @@ it's more like the php template engineer smarty.
```

- if condition

```php
{%if $num > 100%}

{%elseif $num < 50%}

{%else%}

{%/if%}
```

- output

```php
{%$item.url%}
```
Expand All @@ -85,20 +87,20 @@ it's more like the php template engineer smarty.
```

- variable define

```php
{%$title = "this is a title"%}
```

- capture
- capture

```php
{%capture "hello"%}
{%/capture%}
{%capture "hello"%}
{%/capture%}
{%$fet.capture.hello%}
```

- static variables
- static variables

```php
{%$fet.capture.xxx%}
Expand All @@ -107,80 +109,84 @@ it's more like the php template engineer smarty.
{%$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
- special variables
```php
{%$ROOT%} // will output $
{%$fet%} // will output .
```

### Expression
1. operators
`+ - * / % ! ** == >= <= != && || & ^`

1. operators
`+ - * / % ! ** == >= <= != && || & ^`

2. keyword operators
`and` && `or` || `not` ! `eq` == `ne` != `gt` > `ge` >= `lt` < `le` <=
`bitor` for "|"
`and` && `or` || `not` ! `eq` == `ne` != `gt` > `ge` >= `lt` < `le` <=
`bitor` for "|"

3. pipe
`|` pipeline funcs
`:` set arguments for pipeline funcs
3. pipe
`|` pipeline funcs
`:` set arguments for pipeline funcs

4. numbers
hex: `0xffff`
octal: `0o777`
binary: `0b1000`
scientific notation `1e10`
4. numbers
hex: `0xffff`
octal: `0o777`
binary: `0b1000`
scientific notation `1e10`

Be careful of the `and` and `or` operators, they don't have short circuit with conditions.
Be careful of the `and` and `or` operators, they don't have short circuit with conditions.

### Characters concat
```php
{% $sayHello = "world" %}
{% "hello `$sayHello`"%} // output "hello world"
```
use ` `` ` for variable or expression in strings. do not use `+`.


### Func Maps
- Math
`min` `max` `floor` `ceil`

- Helpers
`number_format` `truncate`

- Assert
`empty`

- Length
`count`

- Output
`safe`
- [view more in funcs.go](./lib/funcs/funcs.go)

### Config types.Mode
- types.Smarty
the variable and field must begin with `$`, use `foreach` tag for loops.


- types.Gofet
the variable and field mustn't begin with `$`, use `for` tag for loops.
### Characters concat

```php
{% $sayHello = "world" %}
{% "hello `$sayHello`"%} // output "hello world"
```

use ` `` ` for variable or expression in strings. do not use `+`.

### Func Maps

- Math
`min` `max` `floor` `ceil`

- Helpers
`number_format` `truncate`

- Assert
`empty`

- Length
`count`

- Output
`safe`
- [view more in funcs.go](./lib/funcs/funcs.go)

### Config types.Mode

- types.Smarty
the variable and field must begin with `$`, use `foreach` tag for loops.

* types.Gofet
the variable and field mustn't begin with `$`, use `for` tag for loops.

### In development
```bash
# 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 watch
```

```bash
# 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 watch
```

### Demo code

```go
package main

Expand All @@ -198,7 +204,7 @@ func main(){
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
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"
Expand All @@ -215,49 +221,47 @@ func main(){
}

```
### API

### API

#### static methods

- `fet.LoadConf(configFile string) (*types.FetConfig, error)`

if you use the command line `fetc` build a config file `fet.config.json`, then you can use `fet.LoadConf` to get the config.
- `fet.LoadConf(configFile string) (*types.FetConfig, error)`

if you use the command line `fetc` build a config file `fet.config.json`, then you can use `fet.LoadConf` to get the config.

- `fet.New(config *types.FetConfig) (instance *Fet, error)`

- `fet.New(config *types.FetConfig) (instance *Fet, error)`

get a fet instance.

#### instance methods

- `instance.Compile(tpl string, createFile bool) (result string, err error) `

compile a template file, if `createFile` is true, will create the compiled file.
- `instance.Compile(tpl string, createFile bool) (result string, err error)`

- `instance.CompileAll() error`

compile all files need to compile.
compile a template file, if `createFile` is true, will create the compiled file.

- `instance.CompileAll() error`

- `instance.Display(tpl string, data interface{}, output io.Wirter) error`
compile all files need to compile.

render the parsed html code into `output`.
* `instance.Display(tpl string, data interface{}, output io.Wirter) error`

- `instance.Fetch(tpl string, data interface{}) (result string, err error)`
render the parsed html code into `output`.

just get the parsed `string` code, it always use `CompileOnline` mode.
* `instance.Fetch(tpl string, data interface{}) (result string, err error)`

just get the parsed `string` code, it always use `CompileOnline` mode.

## Use in project

1. `compile mode`
1. `compile mode`

just use fet compile your template files offline, and add the FuncMap `lib/funcs/funcs.go` to your project.

2. `install mode`
2. `install mode`

install `fet`,and use `fet.Display(tpl, data, io.Writer)` to render the template file.


## License

[MIT License](./LICENSE).
Loading

0 comments on commit a72ff73

Please sign in to comment.