{"id":13764030,"url":"https://github.com/gobuffalo/velvet","last_synced_at":"2025-04-11T23:06:34.804Z","repository":{"id":57496919,"uuid":"77626334","full_name":"gobuffalo/velvet","owner":"gobuffalo","description":"A sweet velvety templating package","archived":false,"fork":false,"pushed_at":"2017-03-20T14:41:20.000Z","size":33,"stargazers_count":73,"open_issues_count":2,"forks_count":11,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-04T11:15:08.751Z","etag":null,"topics":["go","golang","handlebars","template","template-engine"],"latest_commit_sha":null,"homepage":null,"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/gobuffalo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-29T16:46:57.000Z","updated_at":"2024-07-09T09:49:50.000Z","dependencies_parsed_at":"2022-09-03T02:10:34.050Z","dependency_job_id":null,"html_url":"https://github.com/gobuffalo/velvet","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/gobuffalo%2Fvelvet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobuffalo%2Fvelvet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobuffalo%2Fvelvet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gobuffalo%2Fvelvet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gobuffalo","download_url":"https://codeload.github.com/gobuffalo/velvet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239805932,"owners_count":19700218,"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","golang","handlebars","template","template-engine"],"created_at":"2024-08-03T15:01:06.475Z","updated_at":"2025-02-20T08:31:43.260Z","avatar_url":"https://github.com/gobuffalo.png","language":"Go","funding_links":[],"categories":["Template Engines","模板引擎","\u003cspan id=\"模板引擎-template-engines\"\u003e模板引擎 Template Engines\u003c/span\u003e","模板引擎`模版渲染和模版生成处理库`","Relational Databases"],"sub_categories":["HTTP Clients","交流","Advanced Console UIs","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","查询语"],"readme":"# Velvet [![GoDoc](https://godoc.org/github.com/gobuffalo/velvet?status.svg)](https://godoc.org/github.com/gobuffalo/velvet) [![Build Status](https://travis-ci.org/gobuffalo/velvet.svg?branch=master)](https://travis-ci.org/gobuffalo/velvet) [![Code Climate](https://codeclimate.com/github/gobuffalo/velvet/badges/gpa.svg)](https://codeclimate.com/github/gobuffalo/velvet)\n\nVelvet is a templating package for Go. It bears a striking resemblance to \"handlebars\" based templates, there are a few small changes/tweaks, that make it slightly different.\n\n## General Usage\n\nIf you know handlebars, you basically know how to use Velvet.\n\nLet's assume you have a template (a string of some kind):\n\n```handlebars\n\u003c!-- some input --\u003e\n\u003ch1\u003e{{ name }}\u003c/h1\u003e\n\u003cul\u003e\n  {{#each names}}\n    \u003cli\u003e{{ @value }}\u003c/li\u003e\n  {{/each}}\n\u003c/ul\u003e\n```\n\nGiven that string, you can render the template like such:\n\n```go\nctx := velvet.NewContext()\nctx.Set(\"name\", \"Mark\")\nctx.Set(\"names\", []string{\"John\", \"Paul\", \"George\", \"Ringo\"})\ns, err := velvet.Render(input, ctx)\nif err != nil {\n  // handle errors\n}\n```\nWhich would result in the following output:\n\n```html\n\u003ch1\u003eMark\u003c/h1\u003e\n\u003cul\u003e\n  \u003cli\u003eJohn\u003c/li\u003e\n  \u003cli\u003ePaul\u003c/li\u003e\n  \u003cli\u003eGeorge\u003c/li\u003e\n  \u003cli\u003eRingo\u003c/li\u003e\n\u003c/ul\u003e\n```\n\n## Helpers\n\n### If Statements\n\nWhat to do? Should you render the content, or not? Using Velvet's built in `if`, `else`, and `unless` helpers, let you figure it out for yourself.\n\n```handlebars\n{{#if true }}\n  render this\n{{/if}}\n```\n\n#### Else Statements\n\n```handlebars\n{{#if false }}\n  won't render this\n{{ else }}\n  render this\n{{/if}}\n```\n\n#### Unless Statements\n\n```handlebars\n{{#unless true }}\n  won't render this\n{{/unless}}\n```\n\n### Each Statements\n\nInto everyone's life a little looping must happen. We can't avoid the need to write loops in applications, so Velvet helps you out by coming loaded with an `each` helper to iterate through `arrays`, `slices`, and `maps`.\n\n#### Arrays\n\nWhen looping through `arrays` or `slices`, the block being looped through will be access to the \"global\" context, as well as have four new variables available within that block:\n\n* `@first` [`bool`] - is this the first pass through the iteration?\n* `@last` [`bool`] - is this the last pass through the iteration?\n* `@index` [`int`] - the counter of where in the loop you are, starting with `0`.\n* `@value` - the current element in the array or slice that is being iterated over.\n\n```handlebars\n\u003cul\u003e\n  {{#each names}}\n    \u003cli\u003e{{ @index }} - {{ @value }}\u003c/li\u003e\n  {{/each}}\n\u003c/ul\u003e\n```\n\nBy using \"block parameters\" you can change the \"key\" of the element being accessed from `@value` to a key of your choosing.\n\n```handlebars\n\u003cul\u003e\n  {{#each names as |name|}}\n    \u003cli\u003e{{ name }}\u003c/li\u003e\n  {{/each}}\n\u003c/ul\u003e\n```\n\nTo change both the key and the index name you can pass two \"block parameters\"; the first being the new name for the index and the second being the name for the element.\n\n```handlebars\n\u003cul\u003e\n  {{#each names as |index, name|}}\n    \u003cli\u003e{{ index }} - {{ name }}\u003c/li\u003e\n  {{/each}}\n\u003c/ul\u003e\n```\n\n#### Maps\n\nLooping through `maps` using the `each` helper is also supported, and follows very similar guidelines to looping through `arrays`.\n\n* `@first` [`bool`] - is this the first pass through the iteration?\n* `@last` [`bool`] - is this the last pass through the iteration?\n* `@key` - the key of the pair being accessed.\n* `@value` - the value of the pair being accessed.\n\n```handlebars\n\u003cul\u003e\n  {{#each users}}\n    \u003cli\u003e{{ @key }} - {{ @value }}\u003c/li\u003e\n  {{/each}}\n\u003c/ul\u003e\n```\n\nBy using \"block parameters\" you can change the \"key\" of the element being accessed from `@value` to a key of your choosing.\n\n```handlebars\n\u003cul\u003e\n  {{#each users as |user|}}\n    \u003cli\u003e{{ @key }} - {{ user }}\u003c/li\u003e\n  {{/each}}\n\u003c/ul\u003e\n```\n\nTo change both the key and the value name you can pass two \"block parameters\"; the first being the new name for the key and the second being the name for the value.\n\n```handlebars\n\u003cul\u003e\n  {{#each users as |key, user|}}\n    \u003cli\u003e{{ key }} - {{ user }}\u003c/li\u003e\n  {{/each}}\n\u003c/ul\u003e\n```\n\n### Other Builtin Helpers\n\n* `json` - returns a JSON marshaled string of the value passed to it.\n* `js_escape` - safely escapes a string to be used in a JavaScript bit of code.\n* `html_escape` - safely escapes a string to be used in an HTML bit of code.\n* `upcase` - upper cases the entire string passed to it.\n* `downcase` - lower cases the entire string passed to it.\n* `markdown` - converts markdown to HTML.\n* `len` - returns the length of an array or slice\n\nVelvet also imports all of the helpers found [https://github.com/markbates/inflect/blob/master/helpers.go](https://github.com/markbates/inflect/blob/master/helpers.go)\n\n## Custom Helpers\n\nNo templating package would be complete without allowing for you to build your own, custom, helper functions.\n\n### Return Values\n\nThe first thing to understand about building custom helper functions is their are a few \"valid\" return values:\n\n#### `string`\n\nReturn just a `string`. The `string` will be HTML escaped, and deemed \"not\"-safe.\n\n```go\nfunc() string {\n  return \"\"\n}\n```\n\n#### `string, error`\n\nReturn a `string` and an error. The `string` will be HTML escaped, and deemed \"not\"-safe.\n\n```go\nfunc() (string, error) {\n  return \"\", nil\n}\n```\n\n#### `template.HTML`\n\n[https://golang.org/pkg/html/template/#HTML](https://golang.org/pkg/html/https://golang.org/pkg/html/template/#HTMLlate/#HTML)\n\nReturn a `template.HTML` string. The `template.HTML` will **not** be HTML escaped, and will be deemed safe.\n\n```go\nfunc() template.HTML {\n  return template.HTML(\"\")\n}\n```\n\n\n#### `template.HTML, error`\n\nReturn a `template.HTML` string and an error. The `template.HTML` will **not** be HTML escaped, and will be deemed safe.\n\n```go\nfunc() ( template.HTML, error ) {\n  return template.HTML(\"\"), error\n}\n```\n\n### Input Values\n\nCustom helper functions can take any type, and any number of arguments. There is an option last argument, [`velvet.HelperContext`](https://godoc.org/github.com/gobuffalo/velvet#HelperContext), that can be received. It's quite useful, and I would recommend taking it, as it provides you access to things like the context of the call, the block associated with the helper, etc...\n\n### Registering Helpers\n\nCustom helpers can be registered in one of two different places; globally and per template.\n\n#### Global Helpers\n\n```go\nerr := velvet.Helpers.Add(\"greet\", func(name string) string {\n  return fmt.Sprintf(\"Hi %s!\", name)\n})\nif err != nil {\n  // handle errors\n}\n```\n\nThe `greet` function is now available to all templates that use Velvet.\n\n```go\ns, err := velvet.Render(`\u003ch1\u003e{{greet \"mark\"}}\u003c/h1\u003e`, velvet.NewContext())\nif err != nil {\n  // handle errors\n}\nfmt.Print(s) // \u003ch1\u003eHi mark!\u003c/h1\u003e\n```\n\n#### Per Template Helpers\n\n```go\nt, err := velvet.Parse(`\u003ch1\u003e{{greet \"mark\"}}\u003c/h1\u003e`)\nif err != nil {\n  // handle errors\n}\nt.Helpers.Add(\"greet\", func(name string) string {\n  return fmt.Sprintf(\"Hi %s!\", name)\n})\nif err != nil {\n  // handle errors\n}\n```\n\nThe `greet` function is now only available to the template it was added to.\n\n```go\ns, err := t.Exec(velvet.NewContext())\nif err != nil {\n  // handle errors\n}\nfmt.Print(s) // \u003ch1\u003eHi mark!\u003c/h1\u003e\n```\n\n### Block Helpers\n\nLike the `if` and `each` helpers, block helpers take a \"block\" of text that can be evaluated and potentially rendered, manipulated, or whatever you would like. To write a block helper, you have to take the `velvet.HelperContext` as the last argument to your helper function. This will give you access to the block associated with that call.\n\n#### Example\n\n```go\nvelvet.Helpers.Add(\"upblock\", func(help velvet.HelperContext) (template.HTML, error) {\n  s, err := help.Block()\n  if err != nil {\n    return \"\", err\n  }\n  return strings.ToUpper(s), nil\n})\n\ns, err := velvet.Render(`{{#upblock}}hi{{/upblock}}`, velvet.NewContext())\nif err != nil {\n  // handle errors\n}\nfmt.Print(s) // HI\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobuffalo%2Fvelvet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgobuffalo%2Fvelvet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgobuffalo%2Fvelvet/lists"}