{"id":13764092,"url":"https://github.com/thedevsaddam/renderer","last_synced_at":"2025-04-06T12:12:01.526Z","repository":{"id":57484368,"uuid":"109877206","full_name":"thedevsaddam/renderer","owner":"thedevsaddam","description":"Simple, lightweight and faster response (JSON, JSONP, XML, YAML, HTML, File) rendering package for Go","archived":false,"fork":false,"pushed_at":"2021-01-18T17:17:13.000Z","size":21,"stargazers_count":262,"open_issues_count":0,"forks_count":28,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-11T17:14:47.036Z","etag":null,"topics":["gorender","goresponse","html","json","jsonp","parsed-template","render","renderer","response","tmpl","xml","yaml"],"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/thedevsaddam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-07T18:53:49.000Z","updated_at":"2024-08-29T02:09:50.000Z","dependencies_parsed_at":"2022-08-26T12:24:14.785Z","dependency_job_id":null,"html_url":"https://github.com/thedevsaddam/renderer","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Frenderer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Frenderer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Frenderer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedevsaddam%2Frenderer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thedevsaddam","download_url":"https://codeload.github.com/thedevsaddam/renderer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478324,"owners_count":20945266,"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":["gorender","goresponse","html","json","jsonp","parsed-template","render","renderer","response","tmpl","xml","yaml"],"created_at":"2024-08-03T15:01:13.575Z","updated_at":"2025-04-06T12:12:01.499Z","avatar_url":"https://github.com/thedevsaddam.png","language":"Go","funding_links":[],"categories":["中间件### 中间件","Web Frameworks","Go","Web框架","Libraries for creating HTTP middlewares","中间件"],"sub_categories":["创建http中间件的代码库","Fail injection","Middlewares","中间件"],"readme":"Package renderer\n==================\n[![Build Status](https://travis-ci.org/thedevsaddam/renderer.svg?branch=master)](https://travis-ci.org/thedevsaddam/renderer)\n[![Project status](https://img.shields.io/badge/version-1.2-green.svg)](https://github.com/thedevsaddam/renderer/releases)\n[![Go Report Card](https://goreportcard.com/badge/github.com/thedevsaddam/renderer)](https://goreportcard.com/report/github.com/thedevsaddam/renderer)\n[![Coverage Status](https://coveralls.io/repos/github/thedevsaddam/renderer/badge.svg?branch=master)](https://coveralls.io/github/thedevsaddam/renderer?branch=master)\n[![GoDoc](https://godoc.org/github.com/thedevsaddam/renderer?status.svg)](https://godoc.org/github.com/thedevsaddam/renderer)\n[![License](https://img.shields.io/dub/l/vibe-d.svg)](https://github.com/thedevsaddam/renderer/blob/dev/LICENSE.md)\n\nSimple, lightweight and faster response (JSON, JSONP, XML, YAML, HTML, File) rendering package for Go\n\n### Installation\n\nInstall the package using\n```go\n$ go get github.com/thedevsaddam/renderer/...\n```\n\n### Usage\n\nTo use the package import it in your `*.go` code\n```go\nimport \"github.com/thedevsaddam/renderer\"\n```\n### Example\n\n```go\npackage main\n\nimport (\n\t\"io\"\n\t\"log\"\n\t\"net/http\"\n\t\"os\"\n\n\t\"github.com/thedevsaddam/renderer\"\n)\n\nfunc main() {\n\trnd := renderer.New()\n\n\tmux := http.NewServeMux()\n\n\tusr := struct {\n\t\tName string\n\t\tAge  int\n\t}{\"John Doe\", 30}\n\n\t// serving String as text/plain\n\tmux.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.String(w, http.StatusOK, \"Welcome to renderer\")\n\t})\n\n\t// serving success but no content\n\tmux.HandleFunc(\"/no-content\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.NoContent(w)\n\t})\n\n\t// serving string as html\n\tmux.HandleFunc(\"/html-string\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.HTMLString(w, http.StatusOK, \"\u003ch1\u003eHello Renderer!\u003c/h1\u003e\")\n\t})\n\n\t// serving JSON\n\tmux.HandleFunc(\"/json\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.JSON(w, http.StatusOK, usr)\n\t})\n\n\t// serving JSONP\n\tmux.HandleFunc(\"/jsonp\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.JSONP(w, http.StatusOK, \"callback\", usr)\n\t})\n\n\t// serving XML\n\tmux.HandleFunc(\"/xml\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.XML(w, http.StatusOK, usr)\n\t})\n\n\t// serving YAML\n\tmux.HandleFunc(\"/yaml\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.YAML(w, http.StatusOK, usr)\n\t})\n\n\t// serving File as arbitary binary data\n\tmux.HandleFunc(\"/binary\", func(w http.ResponseWriter, r *http.Request) {\n\t\tvar reader io.Reader\n\t\treader, _ = os.Open(\"../README.md\")\n\t\trnd.Binary(w, http.StatusOK, reader, \"readme.md\", true)\n\t})\n\n\t// serving File as inline\n\tmux.HandleFunc(\"/file-inline\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.FileView(w, http.StatusOK, \"../README.md\", \"readme.md\")\n\t})\n\n\t// serving File as attachment\n\tmux.HandleFunc(\"/file-download\", func(w http.ResponseWriter, r *http.Request) {\n\t\trnd.FileDownload(w, http.StatusOK, \"../README.md\", \"readme.md\")\n\t})\n\n\t// serving File from reader as inline\n\tmux.HandleFunc(\"/file-reader\", func(w http.ResponseWriter, r *http.Request) {\n\t\tvar reader io.Reader\n\t\treader, _ = os.Open(\"../README.md\")\n\t\trnd.File(w, http.StatusOK, reader, \"readme.md\", true)\n\t})\n\n\t// serving custom response using render and chaining methods\n\tmux.HandleFunc(\"/render\", func(w http.ResponseWriter, r *http.Request) {\n\t\tw.Header().Set(renderer.ContentType, renderer.ContentText)\n\t\trnd.Render(w, http.StatusOK, []byte(\"Send the message as text response\"))\n\t})\n\n\tport := \":9000\"\n\tlog.Println(\"Listening on port\", port)\n\thttp.ListenAndServe(port, mux)\n}\n\n```\n\n### How to render html template?\n\nWell, you can parse html template using `HTML`, `View`, `Template` any of these method. These are based on `html/template` package.\n\nWhen using `Template` method you can simply pass the base layouts, templates path as a slice of string.\n\n***Template example***\n\nYou can parse template on the fly using `Template` method. You can set delimiter, inject FuncMap easily.\n\ntemplate/layout.tmpl\n```html\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003ctitle\u003e{{ template \"title\" . }}\u003c/title\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    {{ template \"content\" . }}\n  \u003c/body\u003e\n  {{ block \"sidebar\" .}}{{end}}\n\u003c/html\u003e\n```\ntemplate/index.tmpl\n```html\n{{ define \"title\" }}Home{{ end }}\n\n{{ define \"content\" }}\n  \u003ch1\u003eHello, {{ .Name | toUpper }}\u003c/h1\u003e\n  \u003cp\u003eLorem ipsum dolor sit amet, consectetur adipisicing elit.\u003c/p\u003e\n{{ end }}\n```\ntemplate/partial.tmpl\n```html\n{{define \"sidebar\"}}\n  simple sidebar code\n{{end}}\n\n```\n\ntemplate.go\n```go\npackage main\n\nimport (\n\t\"html/template\"\n\t\"log\"\n\t\"net/http\"\n\t\"strings\"\n\n\t\"github.com/thedevsaddam/renderer\"\n)\n\nvar rnd *renderer.Render\n\nfunc init() {\n\trnd = renderer.New()\n}\n\nfunc toUpper(s string) string {\n\treturn strings.ToUpper(s)\n}\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n\tusr := struct {\n\t\tName string\n\t\tAge  int\n\t}{\"john doe\", 30}\n\n\ttpls := []string{\"template/layout.tmpl\", \"template/index.tmpl\", \"template/partial.tmpl\"}\n\trnd.FuncMap(template.FuncMap{\n\t\t\"toUpper\": toUpper,\n\t})\n\terr := rnd.Template(w, http.StatusOK, tpls, usr)\n\tif err != nil {\n\t\tlog.Fatal(err) //respond with error page or message\n\t}\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", handler)\n\tlog.Println(\"Listening port: 9000\")\n\thttp.ListenAndServe(\":9000\", nil)\n}\n```\n\n***HTML example***\n\nWhen using `HTML` you can parse a template directory using `pattern` and call the template by their name. See the example code below:\n\nhtml/index.html\n```html\n{{define \"indexPage\"}}\n    \u003chtml\u003e\n    {{template \"header\"}}\n    \u003cbody\u003e\n        \u003ch3\u003eIndex\u003c/h3\u003e\n        \u003cp\u003eLorem ipsum dolor sit amet, consectetur adipisicing elit\u003c/p\u003e\n    \u003c/body\u003e\n    {{template \"footer\"}}\n    \u003c/html\u003e\n{{end}}\n```\n\nhtml/header.html\n```html\n{{define \"header\"}}\n     \u003chead\u003e\n         \u003ctitle\u003eHeader\u003c/title\u003e\n         \u003ch1\u003eHeader section\u003c/h1\u003e\n     \u003c/head\u003e\n{{end}}\n```\n\nhtml/footer.html\n```html\n{{define \"footer\"}}\n     \u003cfooter\u003eCopyright \u0026copy; 2020\u003c/footer\u003e\n{{end}}\n```\n\nhtml.go\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/thedevsaddam/renderer\"\n)\n\nvar rnd *renderer.Render\n\nfunc init() {\n\trnd = renderer.New(\n\t\trenderer.Options{\n\t\t\tParseGlobPattern: \"html/*.html\",\n\t\t},\n\t)\n}\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n\terr := rnd.HTML(w, http.StatusOK, \"indexPage\", nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", handler)\n\tlog.Println(\"Listening port: 9000\")\n\thttp.ListenAndServe(\":9000\", nil)\n}\n```\n\n***View example***\n\nWhen using `View` for parsing template you can pass multiple layout and templates. Here template name will be the file name. See the example to get the idea.\n\nview/base.lout\n```html\n\u003chtml\u003e\n  \u003chead\u003e\n     \u003ctitle\u003e{{block \"title\" .}} {{end}}\u003c/title\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    {{ template \"content\" . }}\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nview/home.tpl\n```html\n{{define \"title\"}}Home{{end}}\n{{define \"content\"}}\n\u003ch3\u003eHome page\u003c/h3\u003e\n    \u003cul\u003e\n        \u003cli\u003e\u003ca href=\"/\"\u003eHome\u003c/a\u003e\u003c/li\u003e\n        \u003cli\u003e\u003ca href=\"/about\"\u003eAbout Me\u003c/a\u003e\u003c/li\u003e\n    \u003c/ul\u003e\n    \u003cp\u003e\n    Lorem ipsum dolor sit amet\u003c/p\u003e\n{{end}}\n```\nview/about.tpl\n```html\n{{define \"title\"}}About Me{{end}}\n{{define \"content\"}}\n\u003ch2\u003eThis is About me page.\u003c/h2\u003e\n\u003cul\u003e\n    Lorem ipsum dolor sit amet, consectetur adipisicing elit,\n\u003c/ul\u003e\n\u003cp\u003e\u003ca href=\"/\"\u003eHome\u003c/a\u003e\u003c/p\u003e\n{{end}}\n```\n\nview.go\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/thedevsaddam/renderer\"\n)\n\nvar rnd *renderer.Render\n\nfunc init() {\n\trnd = renderer.New(renderer.Options{\n\t\tTemplateDir: \"view\",\n\t})\n}\n\nfunc home(w http.ResponseWriter, r *http.Request) {\n\terr := rnd.View(w, http.StatusOK, \"home\", nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc about(w http.ResponseWriter, r *http.Request) {\n\terr := rnd.View(w, http.StatusOK, \"about\", nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", home)\n\thttp.HandleFunc(\"/about\", about)\n\tlog.Println(\"Listening port: 9000\\n / is root \\n /about is about page\")\n\thttp.ListenAndServe(\":9000\", nil)\n}\n```\n\n***Note:*** This is a wrapper on top of go built-in packages to provide syntactic sugar.\n\n### Contribution\nYour suggestions will be more than appreciated.\n[Read the contribution guide here](CONTRIBUTING.md)\n\n### See all [contributors](https://github.com/thedevsaddam/renderer/graphs/contributors)\n\n### Read [API doc](https://godoc.org/github.com/thedevsaddam/renderer) to know about ***Available options and Methods***\n\n### **License**\nThe **renderer** is an open-source software licensed under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedevsaddam%2Frenderer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthedevsaddam%2Frenderer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedevsaddam%2Frenderer/lists"}