{"id":20534234,"url":"https://github.com/gookit/easytpl","last_synced_at":"2025-06-15T00:07:46.523Z","repository":{"id":33055125,"uuid":"144945851","full_name":"gookit/easytpl","owner":"gookit","description":"Simple and easy-to-use template renderer, based on Golang html/template package. 简单易用的模板渲染工具库，基于Golang的 html/template 包，支持布局文件渲染，支持加载多目录，多文件，渲染字符串模板等。","archived":false,"fork":false,"pushed_at":"2025-05-08T12:01:54.000Z","size":121,"stargazers_count":6,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-08T13:20:06.843Z","etag":null,"topics":["layout","template","template-engine","template-layout","template-rendering","view-renderer"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gookit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-08-16T06:23:34.000Z","updated_at":"2025-05-08T12:01:53.000Z","dependencies_parsed_at":"2024-03-11T02:50:21.077Z","dependency_job_id":"c29dc930-7fe9-4ff5-8d08-c9931a3ba436","html_url":"https://github.com/gookit/easytpl","commit_stats":null,"previous_names":["gookit/easytpl","gookit/view"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/gookit/easytpl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gookit%2Feasytpl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gookit%2Feasytpl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gookit%2Feasytpl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gookit%2Feasytpl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gookit","download_url":"https://codeload.github.com/gookit/easytpl/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gookit%2Feasytpl/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259901380,"owners_count":22929224,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["layout","template","template-engine","template-layout","template-rendering","view-renderer"],"created_at":"2024-11-16T00:25:54.487Z","updated_at":"2025-06-15T00:07:46.502Z","avatar_url":"https://github.com/gookit.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasyTpl\n\n[![GoDoc](https://pkg.go.dev/badge/github.com/gookit/easytpl.svg)](https://pkg.go.dev/github.com/gookit/easytpl)\n[![Coverage Status](https://coveralls.io/repos/github/gookit/easytpl/badge.svg?branch=master)](https://coveralls.io/github/gookit/easytpl?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/easytpl)](https://goreportcard.com/report/github.com/gookit/easytpl)\n[![Unit-Tests](https://github.com/gookit/easytpl/workflows/Unit-Tests/badge.svg)](https://github.com/gookit/easytpl/actions)\n\nA simple template renderer based on the Go `html/template`, but much simpler to use. \nSupport layout rendering, including templates.\n\n\u003e **[中文说明](README.zh-CN.md)**\n\n## Features\n\n- simple to use\n- support loading multiple directories, multiple files\n- support rendering string templates, etc.\n- support layout render. \n  - eg `{{ include \"header\" }} {{ yield }} {{ include \"footer\" }}`\n- support include other templates. eg `{{ include \"other\" }}`\n- support `extends` base templates. eg `{{ extends \"base.tpl\" }}`\n- support custom template functions\n- built-in some helper methods `row`, `lower`, `upper`, `join` ...\n\n## Godoc\n\n- [godoc for github](https://pkg.go.dev/github.com/gookit/easytpl)\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\n\t\"github.com/gookit/easytpl\"\n)\n\nfunc main()  {\n\t// equals to call: easytpl.NewRenderer() + r.MustInit()\n\tr := easytpl.NewInited(func(r *easytpl.Renderer) {\n\t\t// setting default layout\n\t\tr.Layout = \"layout\" // equals to \"layout.tpl\"\n\t\t// templates dir. will auto load on init.\n\t\tr.ViewsDir = \"testdata\"\n\t\t// add template function\n\t\tr.AddFunc(\"myFunc\", func() string {\n\t\t\treturn \"my-func\"\n\t\t})\n\t})\n\n\t// fmt.Println(r.TemplateNames(true))\n\n\tbf := new(bytes.Buffer)\n\n\t// render template string\n\tr.String(bf, `hello {{.}}`, \"tom\")\n\tfmt.Print(bf.String()) // hello tom\n\n\t// render template without layout\n\tr.Partial(bf, \"home\", \"tom\")\n\tbf.Reset()\n\n\t// render with default layout\n\tr.Render(bf, \"home\", \"tom\")\n\tbf.Reset()\n\n\t// render with custom layout\n\tr.Render(bf, \"home\", \"tom\", \"site/layout\")\n\tbf.Reset()\n\t\n\t// load named string template \n\tr.LoadString(\"my-page\", \"welcome {{.}}\")\n\t// now, you can use \"my-page\" as an template name\n\tr.Partial(bf, \"my-page\", \"tom\") // welcome tom\n\tbf.Reset()\n\t\n\t// more ways for load templates\n\tr.LoadByGlob(\"some/path/*\", \"some/path\")\n\tr.LoadFiles(\"path/file1.tpl\", \"path/file2.tpl\")\n}\n```\n\n\u003e more APIs please [GoDoc](https://pkg.go.dev/github.com/gookit/easytpl) \n\n## Layout Example\n\nbasic layout structure:\n\n```text\n{{ include \"part0\" }}{{ yield }}{{ include \"part1\" }}\n```\n\n\u003e current template will render at `{{ yield }}`\n\nexample files:\n\n```text\ntemplates/\n  |_ layouts/\n  |    |_ default.tpl\n  |    |_ header.tpl\n  |    |_ footer.tpl\n  |_ home.tpl\n  |_ about.tpl\n```\n\n- **layout:** `templates/layouts/default.tpl`\n\n```html\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003ctitle\u003elayout example\u003c/title\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003c!-- include \"layouts/header.tpl\" --\u003e\n    {{ include \"header\" }}\n    \u003c!-- Render the current template here --\u003e\n    {{ yield }}\n    \u003c!-- include \"layouts/footer.tpl\" --\u003e\n    {{ include \"footer\" }}\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n- `templates/layouts/header.tpl`\n\n```html\n\u003cheader\u003e\n    \u003ch2\u003epage header\u003c/h2\u003e\n\u003c/header\u003e\n```\n\n- `templates/layouts/footer.tpl`\n\n```html\n\u003cfooter\u003e\n    \u003ch2\u003epage footer\u003c/h2\u003e\n\u003c/footer\u003e\n```\n\n- `templates/home.tpl`\n\n```gotemplate title=\"home.tpl\"\n  \u003ch1\u003eHello, {{ .Name | upper }}\u003c/h1\u003e\n  \u003ch2\u003eAt template {{ current_tpl }}\u003c/h2\u003e\n  \u003cp\u003eLorem ipsum dolor sit amet, consectetur adipisicing elit.\u003c/p\u003e\n```\n\n### Usage\n\n```go\nv := easytpl.NewInited(func(r *easytpl.Renderer) {\n    // setting default layout\n    r.Layout = \"layouts/default\" // equals to \"layouts/default.tpl\"\n    // templates dir. will auto load on init.\n    r.ViewsDir = \"templates\"\n})\n\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\tv.Render(w, \"home\", easytpl.M{\"Name\": \"tom\"})\n})\nslog.Println(\"Listening port: 9100\")\nhttp.ListenAndServe(\":9100\", nil)\n```\n\n## `extends` example\n\nA base template can be inherited using the `{{ extends \"base.tpl\" }}` statement.\n\n\u003e Note: The `extends` statement must be on the first line of the template file\n\n```text\ntemplates/\n  |_ base.tpl\n  |_ home.tpl\n  |_ about.tpl\n```\n\n`templates/base.tpl` base template contents:\n\n```gotemplate title=\"base.tpl\"\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\n    \u003ctitle\u003elayout example\u003c/title\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    {{ block \"content\" . }}\n    \u003ch1\u003eHello, at base template\u003c/h1\u003e\n    {{ end }}\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n`templates/home.tpl` contents:\n\n```gotemplate title=\"home.tpl\"\n{{ extends \"base\" }}\n\n{{ define \"content\" }}\n  \u003ch1\u003eHello, {{ .Name | upper }}\u003c/h1\u003e\n  \u003ch2\u003eAt template {{ current_tpl }}\u003c/h2\u003e\n  \u003cp\u003eLorem ipsum dolor sit amet, consectetur adipisicing elit.\u003c/p\u003e\n{{ end }}\n```\n\n### Usage\n\n```go\npackage main\n\nimport (\n    \"net/http\"\n    \n    \"github.com/gookit/easytpl\"\n    \"github.com/gookit/slog\"\n)\n\nfunc main() {\n    v := easytpl.NewExtends(easytpl.WithTplDirs(\"templates\"))\n\n    http.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n        v.Render(w, \"home\", easytpl.M{\"Name\": \"tom\"})\n    })\n    \n    slog.Info(\"Listening port: 9100\")\n    http.ListenAndServe(\":9100\", nil)\n}\n```\n\n## Available Options\n\n```go\n// Debug setting\nDebug bool\n// Layout template name\nLayout string\n// Delims define for template\nDelims TplDelims\n// ViewsDir the default views directory, multi use \",\" split\nViewsDir string\n// ExtNames allowed template extensions. eg {\"tpl\", \"html\"}\nExtNames []string\n// FuncMap func map for template\nFuncMap template.FuncMap\n// DisableLayout disable layout. default is False\nDisableLayout bool\n// AutoSearchFile auto search template file, when not found on compiled templates. default is False\nAutoSearchFile bool\n```\n\n### Apply options\n\n- method 1\n\n```go\nr := easytpl.NewRenderer()\nr.Layout = \"layouts/default\"\n// ... ...\nr.MustInit()\n```\n\n- method 2\n\n```go\nr := easytpl.NewRenderer(func (r *Renderer) {\n\tr.Layout = \"layouts/default\"\n\t// ... ...\n})\nr.MustInit()\n```\n\n- method 3\n\n```go\nr := easytpl.NewInited(func (r *Renderer) {\n\tr.Layout = \"layouts/default\" \n\t// ... ...\n})\n```\n\n## Reference\n\n- https://github.com/unrolled/render\n- https://github.com/thedevsaddam/renderer\n\n## License\n\n**MIT**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgookit%2Feasytpl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgookit%2Feasytpl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgookit%2Feasytpl/lists"}