{"id":18073366,"url":"https://github.com/blakewilliams/glam","last_synced_at":"2025-04-05T18:16:47.172Z","repository":{"id":255604222,"uuid":"851205924","full_name":"BlakeWilliams/glam","owner":"BlakeWilliams","description":"Go's `html/template` with a component focus","archived":false,"fork":false,"pushed_at":"2024-10-21T00:27:40.000Z","size":101,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T01:41:23.591Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BlakeWilliams.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2024-09-02T16:16:56.000Z","updated_at":"2024-10-21T00:27:43.000Z","dependencies_parsed_at":"2024-10-20T18:15:01.287Z","dependency_job_id":null,"html_url":"https://github.com/BlakeWilliams/glam","commit_stats":null,"previous_names":["blakewilliams/goat","blakewilliams/glam"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlakeWilliams%2Fglam","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlakeWilliams%2Fglam/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlakeWilliams%2Fglam/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlakeWilliams%2Fglam/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BlakeWilliams","download_url":"https://codeload.github.com/BlakeWilliams/glam/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247378141,"owners_count":20929296,"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-10-31T10:06:36.326Z","updated_at":"2025-04-05T18:16:47.149Z","avatar_url":"https://github.com/BlakeWilliams.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Glam\n\nGlam is an attempt to make Go templates more component focused using the constraints of the existing Go language and tooling.\n\nWith glam, you can write templates like:\n\n```html\n\u003c!-- Initializes and renders a `type FormComponent struct` --\u003e\n\u003cFormComponent action=\"{{ .URL }}\"\u003e\n  \u003c!-- Initializes and renders the `type TextField struct` --\u003e\n  \u003cTextField placeholder=\"username\" prefix=\"@\" value=\"{{.Username}}\" /\u003e\n  \u003cTextField placeholder=\"email\" value=\"{{.Email}}\" /\u003e\n  \u003cTextField placeholder=\"password\" type=\"password\" /\u003e\n\n  \u003cbutton type=\"submit\" class=\"btn\"\u003eSign Up\u003c/button\u003e\n\u003c/FormComponent\u003e\n```\n\nWith structs backing the logic and templates:\n\n```go\ntype FormComponent struct {\n\tAction   string        `attr:\"action\"`\n\tChildren template.HTML\n\tClass    string        `attr:\"class\"`\n}\n```\n\n```html\n\u003c!-- FormComponent template --\u003e\n\u003cform action=\"{{.Action}}\" class=\"default classes {{.Class}}\"\u003e\n  {{.Children}}\n\u003c/form\u003e\n```\n\n## Usage\n\nGlam takes an approach similar to [ViewComponent](https://viewcomponent.org/), using sidecar templates to define components. In addition to coupling Go templates with structs, glam also allows enables a React style syntax for utilizing components in your templates.\n\n```go\nimport \"github.com/blakewilliams/glam\"\n\ntype GreetPage struct {\n\tName string\n}\n\n// a Helper method we can use in our template\nfunc (g GreetPage) YellName() string {\n\treturn strings.ToUpper(g.Name)\n}\n\n// Then, to render the template:\nengine := glam.New(nil)\nengine.RegisterComponent(GreetPage{}, `Hello, {{.YellName}}`)\n\nvar b strings.Builder\nengine.Render(\u0026b, \u0026GreetPage{Name: \"World\"})\n```\n\nThe `GreetPage` struct instance will be passed to the `greet_page.html` template as `data`, allowing you to access public fields and call methods on the struct.\n\n### Composing components\n\nLet's say we want to reuse our YellName functionality in another component, but also **bold** the name. We can create a new component and reference the component directly in our `greet_page.html` template as if it was another element:\n\n```go\ntype Yell struct {\n\tName string\n}\n\nfunc (y Yell) YellName() string {\n\treturn strings.ToUpper(y.Name)\n}\n\n// Lets update our GreetPage component to use our new Yell component\nengine.RegisterComponent(GreetPage{}, `Hello, \u003cYell Name={{.Name}}\u003e\u003c/Yell\u003e`)\n// Let's also register our new Yell component\nengine.RegisterComponent(Yell{}, `\u003cb\u003e{{.YellName}}\u003c/b\u003e`)\n```\n\nThe HTML is parsed and the `Yell` HTML tag is replaced with a call to render our Yell component.\n\n### Child content\n\nSince components can be used like HTML tags, that means they can have child content too. The current approach is relatively basic since it always expects a `template.HTML` value, but you can accept and render child content using the conventional `Children` struct field:\n\n```go\ntype WrapperComponent struct {\n\tChildren template.HTML\n}\n```\n\n```html\n\u003cWrapperComponent\u003eHello\u003c/WrapperComponent\u003e\n```\n\nWhen the template above is executed, `WrapperComponent` will have `Children` populated with the HTML safe string `Hello`.\n\n### Request specific data\n\nGlam templates can utilize request specific data via `RenderWithFuncs`:\n\n```go\nengine := glam.New(glam.FuncMap{\n\t\"CSRF\": func() string {\n\t\t// assuming request is in scope and has a CSRFToken method\n\t\tpanic(\"must be overridden\")\n\t},\n})\nengine.RegisterComponent(\u0026LoginForm{}, \"\u003cform\u003e\u003cinput type='hidden' name='authenticity_token' value='{{ CSRF }}' /\u003e\u003c/form\u003e\")\n\nvar b strings.Builder\nengine.RenderWithFuncs(\u0026b, \u0026LoginForm{}, glam.FuncMap{\n\t\"CSRF\": func() string {\n\t\t// assuming request is in scope and has a CSRFToken method\n\t\treturn request.CSRFToken()\n\t},\n})\n````\n\n### Graceful degradation\n\nComponents can implement the `Recoverable` interface to rescue against `panic`s and render fallback content. For example:\n\n```go\ntype SafeSidebar struct {\n\tChildren template.HTML\n}\n\nfunc (mc *SafeSidebar) Recover(w io.Writer, err any) {\n\tw.Write(`\u003cb\u003eFailed to load sidebar\u003c/b\u003e`)\n}\n```\n\nIf any `panic` occurs when rendering `SafeSidebar` or child content (via `\u003cSafeSidebar\u003efoo bar\u003c/SafeSidebar\u003e`) it will render the fallback content written.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblakewilliams%2Fglam","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblakewilliams%2Fglam","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblakewilliams%2Fglam/lists"}