{"id":13676730,"url":"https://github.com/kataras/blocks","last_synced_at":"2025-04-13T00:42:52.598Z","repository":{"id":43455141,"uuid":"285120254","full_name":"kataras/blocks","owner":"kataras","description":"Go-idiomatic View Engine","archived":false,"fork":false,"pushed_at":"2024-12-15T14:39:01.000Z","size":65,"stargazers_count":73,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-13T00:42:46.864Z","etag":null,"topics":["go","iris","template","views"],"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/kataras.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"kataras"}},"created_at":"2020-08-04T23:01:11.000Z","updated_at":"2025-02-26T21:53:19.000Z","dependencies_parsed_at":"2024-12-27T06:07:46.805Z","dependency_job_id":null,"html_url":"https://github.com/kataras/blocks","commit_stats":{"total_commits":31,"total_committers":2,"mean_commits":15.5,"dds":"0.032258064516129004","last_synced_commit":"e27c9ede8236bdd56de56fa7e3c72950812d1ba8"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Fblocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Fblocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Fblocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kataras%2Fblocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kataras","download_url":"https://codeload.github.com/kataras/blocks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650417,"owners_count":21139672,"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":["go","iris","template","views"],"created_at":"2024-08-02T13:00:31.879Z","updated_at":"2025-04-13T00:42:52.579Z","avatar_url":"https://github.com/kataras.png","language":"Go","funding_links":["https://github.com/sponsors/kataras"],"categories":["HTML template engines"],"sub_categories":[],"readme":"# Blocks\n\n[![build status](https://img.shields.io/github/actions/workflow/status/kataras/blocks/ci.yml?style=for-the-badge)](https://github.com/kataras/blocks/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/blocks) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://pkg.go.dev/github.com/kataras/blocks)\n\nBlocks is a, simple, Go-idiomatic view engine based on [html/template](https://pkg.go.dev/html/template?tab=doc#Template), plus the following features:\n\n- Compatible with the [fs.FS](https://pkg.go.dev/io/fs#FS), [embed.FS](https://pkg.go.dev/embed#FS) and [http.FileSystem](https://pkg.go.dev/net/http#FileSystem) interface\n- Embedded templates through [embed.FS](https://pkg.go.dev/embed#FS) or [go-bindata](https://github.com/go-bindata/go-bindata)\n- Load with optional context for cancelation\n- Reload templates on development stage\n- Full Layouts and Blocks support\n- Automatic HTML comments removal\n- Memory File System\n- Markdown Content\n- Global [FuncMap](https://pkg.go.dev/html/template?tab=doc#FuncMap)\n\n## Installation\n\nThe only requirement is the [Go Programming Language](https://go.dev/dl).\n\n```sh\n$ go get github.com/kataras/blocks@latest\n```\n\n## Getting Started\n\n\u003e Extensive and thorough **[documentation](DOC.md)** making it easy to get started with Blocks.\n\nCreate a folder named **./views** and put some HTML template files.\n\n```\n│   main.go\n└───views\n    |   index.html\n    ├───layouts\n    │       main.html\n    ├───partials\n    │       footer.html\n```\n\nNow, open the **./views/layouts/main.html** file and paste the following:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003e{{ if .Title }}{{ .Title }}{{ else }}Default Main Title{{ end }}\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    {{ template \"content\" . }}\n\n\u003cfooter\u003e{{ partial \"partials/footer\" .}}\u003c/footer\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nThe `template \"content\" .` is a Blocks builtin template function which renders the **main** content of your page that should be rendered under this layout. The block name should always be `\"content\"`.\n\nThe `partial` is a Blocks builtin template function which renders a template inside other template.\n\nThe `.` (dot) passes the template's root binding data to the included templates.\n\nContinue by creating the **./views/index.html** file, copy-paste the following markup:\n\n```html\n\u003ch1\u003eIndex Body\u003c/h1\u003e\n```\n\nIn order to render `index` on the `main` layout, create the `./main.go` file by following the below.\n\nImport the package:\n\n```go\nimport \"github.com/kataras/blocks\"\n```\n\nThe `blocks` package is fully compatible with the standard library. Use the [New(directory string)](https://pkg.go.dev/github.com/kataras/blocks?tab=doc#New) function to return a fresh Blcoks view engine that renders templates. \n\nThis directory can be used to locate system template files or to select the wanted template files across a range of embedded data (or empty if templates are not prefixed with a root directory).\n\n```go\nviews := blocks.New(\"./views\")\n```\n\nThe default layouts directory is `$dir/layouts`, you can change it by `blocks.New(...).LayoutDir(\"otherLayouts\")`.\n\nTo parse files that are translated as Go code, inside the executable program itself, pass the [go-bindata's generated](https://github.com/go-bindata/go-bindata) latest version's `AssetFile` method to the `New` function:\n\n```sh\n$ go get -u github.com/go-bindata/go-bindata\n```\n\n```go\nviews := blocks.New(AssetFile())\n```\n\nAfter the initialization and engine's customizations the user SHOULD call its [Load() error](https://pkg.go.dev/github.com/kataras/blocks?tab=doc#Blocks.Load) or [LoadWithContext(context.Context) error](https://pkg.go.dev/github.com/kataras/blocks?tab=doc#Blocks.LoadWithContext) method once in order to parse the files into templates.\n\n```go\nerr := views.Load()\n```\n\nTo render a template through a compatible [io.Writer](https://golang.org/pkg/io/#Writer) use the [ExecuteTemplate(w io.Writer, tmplName, layoutName string, data any)](https://pkg.go.dev/github.com/kataras/blocks?tab=doc#Blocks.ExecuteTemplate) method.\n\n```go\nfunc handler(w http.ResponseWriter, r *http.Request) {\n\tdata := map[string]any{\n\t\t\"Title\": \"Index Title\",\n\t}\n\n\terr := views.ExecuteTemplate(w, \"index\", \"main\", data)\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n}\n```\n\nThere are several methods to customize the engine, **before `Load`**, including `Delims`, `Option`, `Funcs`, `Extension`, `RootDir`, `LayoutDir`, `LayoutFuncs`, `DefaultLayout` and `Extensions`. You can learn more about those in our [godocs](https://pkg.go.dev/github.com/kataras/blocks?tab=Blocks).\n\nPlease navigate through [_examples](_examples) directory for more.\n\n## License\n\nThis software is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkataras%2Fblocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkataras%2Fblocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkataras%2Fblocks/lists"}