{"id":21275680,"url":"https://github.com/halimath/decor","last_synced_at":"2025-03-15T13:13:45.085Z","repository":{"id":57615733,"uuid":"382833771","full_name":"halimath/decor","owner":"halimath","description":"A template loader and cache for Go (golang)","archived":false,"fork":false,"pushed_at":"2022-07-10T15:43:36.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-22T03:27:31.642Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/halimath.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}},"created_at":"2021-07-04T11:24:08.000Z","updated_at":"2022-07-10T15:40:13.000Z","dependencies_parsed_at":"2022-09-14T12:51:10.751Z","dependency_job_id":null,"html_url":"https://github.com/halimath/decor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Fdecor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Fdecor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Fdecor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Fdecor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/halimath","download_url":"https://codeload.github.com/halimath/decor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243732303,"owners_count":20338839,"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":[],"created_at":"2024-11-21T09:36:09.021Z","updated_at":"2025-03-15T13:13:45.064Z","avatar_url":"https://github.com/halimath.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# decor\n\n![CI Status][ci-img-url] [![Go Report Card][go-report-card-img-url]][go-report-card-url] [![Package Doc][package-doc-img-url]][package-doc-url] [![Releases][release-img-url]][release-url]\n\n`decor` contains a simple-to-user yet powerful template loader\nfor [Golang](https://golang.org) mainly focussed on the built-in \npackages `text/template` and `html/template`.\n\nWhile these packages provide powerful templating facilities with\ngreat support for rendering textual content, using these packages\nto build a complete layout-based template solution involves a lot\nof boilerplate code. decor tries to fill this gap by providing just\nwhat's needed to reduce the boilerplate and enable a great developer\nexperience.\n\n## Installation\n\n```\n$ go get github.com/halimath/decor\n```\n\nDecor requires go \u003e= 1.16.\n\n## Usage\n\nThe core components provided by `decor` is the `Templates` struct which\nprovides the rendering functionality of templates. When creating an instance\nyou need to provide a `Loader` which is responsible for loading a named template.\n\n`decor` provides a files loader for both `text` and `html` templates that includes\ncapabilities to load layout files as well.\n\n```go\ntpls := decor.Templates{\n    Includes: []string{\n        \"layouts/base\",\n    },\n    Loader: text.NewFilesLoader(\"%s.txt\", \"testtemplates\"),\n}\n\nif err := tpls.ExecuteTemplate(os.Stdout, \"a\", \"world\"); err != nil {\n    log.Fatal(err)\n}\n```\n\nThe above snippet configures a `Templates` value and effectively executes something like.\n\n```go\nt, _ := \"text/template\".ParseFiles(\"testtemplates/a.txt\", \"testtemplates/layouts/base.txt\")\nt.Execute(os.Stdout, \"world\")\n```\n\nNote that error handling as well as template caching is omitted from the example; the two lines above\nonly sketch what's happening.\n\n### Caching Templates\n\nBy default templates loaded are put into a cache the first time they are loaded. The `Templates` struct\nis safe to use across multiple goroutines, so loading and rendering template in parallel works out of the\nbox. Internally, `Templates` uses an efficient `sync.RWLock` to guard access to the thread.\n\nYou can put `Templates` into _development mode_ by setting its `DevelMode`-Field to `true`. This \ncompletely disables the cache and templates are loaded using the loader _every time_ they are executed. \nThis is great during development when changes made to templates become immediately effective.\n\n### Rendering HTTP Response\n\nIts a common case to render a template producing HTML as part of a web server application using the \n`net/http` package. `decor` provides a utility function to handle this case as a one liner:\n`Templates.SendHTML`. The method accepts a `http.ResponseWriter`, a template name and template data\nand directly writes any output rendered from template to the HTTP response. The method also sets the\ncontent type to `text/html` as well as the content length.\n\nWhen in development mode (see above), any errors generated during rendering will be send to the HTTP \nclient for debugging. When not in development mode, errors will be logged and the response will be empty.\n\n## Development\n\nThe files in package `html` are generated from the corresponding files in package `text`. The code \ngeneration tool can be found in `cmd/gen-html`. \n\n## License\n\nCopyright 2021 Alexander Metzner\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n[ci-img-url]: https://github.com/halimath/decor/workflows/CI/badge.svg\n[go-report-card-img-url]: https://goreportcard.com/badge/github.com/halimath/decor\n[go-report-card-url]: https://goreportcard.com/report/github.com/halimath/decor\n[package-doc-img-url]: https://img.shields.io/badge/GoDoc-Reference-blue.svg\n[package-doc-url]: https://pkg.go.dev/github.com/halimath/decor\n[release-img-url]: https://img.shields.io/github/v/release/halimath/decor.svg\n[release-url]: https://github.com/halimath/decor/releases\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalimath%2Fdecor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhalimath%2Fdecor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalimath%2Fdecor/lists"}