{"id":22598517,"url":"https://github.com/nilorg/ngin","last_synced_at":"2025-03-28T19:30:04.870Z","repository":{"id":36084701,"uuid":"175922591","full_name":"nilorg/ngin","owner":"nilorg","description":"Web framework 包装一些gin功能。","archived":false,"fork":false,"pushed_at":"2022-04-21T03:51:53.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T21:30:12.279Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nilorg.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":"2019-03-16T03:42:03.000Z","updated_at":"2022-04-21T03:51:47.000Z","dependencies_parsed_at":"2022-08-08T13:15:14.510Z","dependency_job_id":null,"html_url":"https://github.com/nilorg/ngin","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/nilorg%2Fngin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilorg%2Fngin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilorg%2Fngin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilorg%2Fngin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilorg","download_url":"https://codeload.github.com/nilorg/ngin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246088220,"owners_count":20721644,"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-12-08T11:06:06.934Z","updated_at":"2025-03-28T19:30:04.846Z","avatar_url":"https://github.com/nilorg.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ngin\nWeb framework 包装一些gin功能。\n\n# 模板结构\n```bash\n./templates\n└── default\n    ├── errors\n    ├── layouts\n    │   ├── layout.tmpl\n    │   └── pjax_layout.tmpl\n    └── pages\n        ├── posts\n        │   ├── posts.tmpl\n        └── posts_detail\n            └── posts_detail.tmpl\n```\n# 安装插件\n```bash\ngo get -u github.com/gin-contrib/multitemplate\n\ngo get -u github.com/nilorg/ngin\n```\n\n# 加载模板\n```go\n// 主题名称\nconst themeName string = \"default\"\n\nfunc loadTemplates(templatesDir string) multitemplate.Render {\n\tr := multitemplate.New()\n\t// 加载布局\n\tlayouts, err := filepath.Glob(filepath.Join(templatesDir, themeName, \"layouts/*.tmpl\"))\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\t// 加载错误页面\n\terrors, err := filepath.Glob(filepath.Join(templatesDir, themeName, \"errors/*.tmpl\"))\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfor _, errPage := range errors {\n\t\ttmplName := fmt.Sprintf(\"error_%s\", filepath.Base(errPage))\n\t\tr.AddFromFiles(tmplName, errPage)\n\t}\n\t// 页面文件夹\n\tpages, err := ioutil.ReadDir(filepath.Join(templatesDir, themeName, \"pages\"))\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfor _, page := range pages {\n\t\tif !page.IsDir() {\n\t\t\tcontinue\n\t\t}\n\t\tfor _, layout := range layouts {\n\t\t\tpageItems, err := filepath.Glob(filepath.Join(templatesDir, themeName, fmt.Sprintf(\"pages/%s/*.tmpl\", page.Name())))\n\t\t\tif err != nil {\n\t\t\t\tpanic(err.Error())\n\t\t\t}\n\t\t\tfiles := []string{\n\t\t\t\tlayout,\n\t\t\t}\n\t\t\tfiles = append(files,pageItems...)\n\t\t\ttmplName := fmt.Sprintf(\"%s_pages_%s\", filepath.Base(layout), page.Name())\n\t\t\tfmt.Println(tmplName, files)\n\t\t\tr.AddFromFiles(tmplName, files...)\n\t\t}\n\t}\n\t// 加载单页面\n\tsingles, err := filepath.Glob(filepath.Join(templatesDir, themeName, \"singles/*.tmpl\"))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfor _, singlePage := range singles {\n\t\ttmplName := fmt.Sprintf(\"singles_%s\", filepath.Base(singlePage))\n\t\tr.AddFromFiles(tmplName, singlePage)\n\t}\n\treturn r\n}\n```\n# 使用模板\n```go\nengine := gin.Default()\nengine.HTMLRender = loadTemplates(\"./templates\")\n```\n# 给路由配置模板\n```go \nimport ngin \"github.com/nilorg/ngin\"\n\nengine.GET(\"/detail\", ngin.WebControllerFunc(func(ctx *ngin.WebContext) {\n\t\tctx.RenderPage(gin.H{\n\t\t\t\"title\":\"标题\",\n\t\t})\n\t}, ngin.PageName(\"posts_detail\")))\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilorg%2Fngin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilorg%2Fngin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilorg%2Fngin/lists"}