Code Monkey home page Code Monkey logo

fet's Introduction

FET

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

FET能按自身支持的语法来将模板编译成text/template的模板语法,当然这只是第一步。

Why FET

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

text/template 作为go官方的模板引擎,虽然基础的功能已有支持,但对于开发人员来说,其书写语法十分原始,写起来很困难,所以这才有了FET。

Usage

it's more like the php template engineer smarty.

使用方式与PHP的模板引擎smarty类似,个别地方有差异

  • [继承]inherit

    {{extends "base.html"}}

  • [继承block块]blocks for inherit

    {{block "header"}}

    <div>some code here</div>

    {{/block}}

  • [引入包含文件]include

    {{include "header.html"}}

  • [循环]loop

    {{for item,key in list}}

    {{/for}}

  • [条件判断]if condition

    {{if num > 100}}

    {{elseif num < 50}}

    {{else}}

    {{/if}}

  • [数据输出]output

    {{item.url}}

  • [使用方法管道]pipe funcs

    {{item.title|truncate:30}}

  • [定义变量]variable assign

    {{title = "this is a title"}}

[表达式支持]Expression

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

  2. pipe
    | and : for arguments

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

[内置的函数]Func Maps

[示例代码]Demo code

package main

import "fet"

func main(){
  conf := &fet.Config{
    TemplateDir: "tmpls", //  default "templates"
    CompileDir: "views", // default "templates_c",
    Ignores: []string{"inc/*"}, // ignore compile paths,files that only will include.use filepath.Match
    LowerField: true, // default false, if true will not translate keys to uppercase.
    CompileOnline: false, // default false, you should compile your template files offline 
  }
  fet, _ := fet.New(conf)
  // compile all files
  fet.CompileAll()
  // data := map[string]string{
  // }
  // fet.Display(tpl, data)
}

API

  • fet.Compile(tpl string, createFile bool) (result string, err error)

    compile a template file, if createFile is true, will create the compiled file.

    编译单个文件,如果createFile参数设为true,将生成对应的编译文件到编译目录里,文件的相对目录路径和原始的fet模板文件相对路径保持一致。

  • fet.CompileAll() error

    compile all files need to compile.

    编译所有需要编译的文件,除了那些在Ignores配置中设置了无需编译的文件。

  • fet.Display(tpl string, data interface{}) error

    render the parsed code into io.Stdout,output it.

    将模板文件渲染后的结果输出显示。

  • fet.Fetch(tpl string, data interface{}) (result string, err error)

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

    获取模板文件渲染后的代码,始终按当前fet模板文件的内容编译,然后渲染得到结果。

[如何在项目中使用]Use in project

  1. [仅编译模式] compile mode
    just use fet compile your template files offline, and add the FuncMap lib/funcs/func.go to html/template struct.

    在项目中,使用fet的方式来编写模板,然后将编写好的模板编译到项目中,引入lib/funcs/func.go里写好的通用方法,由go后端同学注册进去,走线下编译模式。

  2. [安装模式] install mode

    install fet,and use fet.Display(tpl) to render the template file.

    将fet安装到项目中,同时需要将fet打包到项目内上线,使用fet的api来渲染输出模板。

License

MIT License.

fet's People

Contributors

fefit avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.