{"id":15091866,"url":"https://github.com/candyframework/candyjs-template-hbs","last_synced_at":"2026-02-09T02:31:00.726Z","repository":{"id":143745578,"uuid":"286206070","full_name":"candyframework/candyjs-template-hbs","owner":"candyframework","description":"为 candyjs 设计的，基于 Handlebars 的模板引擎","archived":false,"fork":false,"pushed_at":"2024-04-29T06:20:56.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-08-07T21:48:37.213Z","etag":null,"topics":["douyu","douyutv","handlebars","mvc"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/candyframework.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":"2020-08-09T09:28:36.000Z","updated_at":"2024-04-29T06:20:59.000Z","dependencies_parsed_at":"2023-04-13T12:05:05.477Z","dependency_job_id":null,"html_url":"https://github.com/candyframework/candyjs-template-hbs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/candyframework/candyjs-template-hbs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fcandyjs-template-hbs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fcandyjs-template-hbs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fcandyjs-template-hbs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fcandyjs-template-hbs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/candyframework","download_url":"https://codeload.github.com/candyframework/candyjs-template-hbs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fcandyjs-template-hbs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29254316,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-09T01:52:29.835Z","status":"online","status_checked_at":"2026-02-09T02:00:09.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["douyu","douyutv","handlebars","mvc"],"created_at":"2024-09-25T11:00:34.657Z","updated_at":"2026-02-09T02:31:00.710Z","avatar_url":"https://github.com/candyframework.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## candyjs 模板引擎\n\n此模板引擎基于 Handlebars 开发，旨在为 candyjs 提供强大的模板处理功能\n\n## 编译\n\n```\n./node_modules/.bin/tsc\n```\n\n## 使用\n\n#### 安装\n\n```\nnpm install @candyjs/template-hbs\n```\n\n#### 直接使用\n\n使用模板引擎的方式很多，具体参考 candyjs 的文档，这里介绍其中一种：全局配置方式\n\n```\n// 入口文件 index.js\nvar CandyJs = require('candyjs');\nvar Candy = require('candyjs/Candy');\nvar App = require('candyjs/web/Application');\n\n// 由于 candyjs 的模板采用别名路径\n// 默认别名路径并不包含 node_modules 所以这里添加一下 方便引入模板引擎\nCandy.setPathAlias('@template', __dirname + '/node_modules');\n\nnew CandyJs(new App({\n    'id': 1,\n    'debug': true,\n    'appPath': __dirname + '/app',\n    // 使用别名路径指定使用的模板引擎\n    'defaultView': 'template/@candyjs/template-hbs/index'\n\n})).listen(2333, function(){\n    console.log('listen on 2333');\n});\n\n\n// app/controllers/index/IndexController.js\nvar CandyJs = require('candyjs');\nvar Controller = require('candyjs/web/Controller');\n\nmodule.exports = class IndexController extends Controller {\n    run(req, res) {\n        // 渲染 index 模板\n        this.render('index', {\n            age: 20\n        })\n    }\n}\n\n\n// app/views/index/index.html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"utf-8\"\u003e\n    \u003ctitle\u003eview demo\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    {{ age }}\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n#### 布局文件的使用\n\n大部分时候网站布局都有一部分保持不变的结构，比如头部导航，底部 footer ，这部分比较固定，可以利用布局文件将这部分逻辑进行抽离，避免重复布局\n\n使用布局文件很简单，只需要编写一个布局文件并修改控制器部分即可\n\n```\n// app/controllers/index/IndexController.js\nvar CandyJs = require('candyjs');\nvar Controller = require('candyjs/web/Controller');\n\nmodule.exports = class IndexController extends Controller {\n    run(req, res) {\n        this.getView().title = 'layout demo';\n        this.getView().enableLayout = true;\n\n        this.render('index', {\n            age: 20\n        });\n    }\n}\n\n\n// app/views/layout.html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"utf-8\"\u003e\n    \u003ctitle\u003e{{ $this.title }}\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003cnav\u003elayout page header\u003c/nav\u003e\n\n    \u003c!-- 由于 contentHtml 包含 html 标签 所以使用三个大括号解析内容 --\u003e\n    {{{ contentHtml }}}\n\n    \u003cfooter\u003elayout page footer\u003c/footer\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n\n\n// app/views/index/index.html\n// 可见现在的模板文件已经没有了 html 文档结构\n// 取而代之的是一个文档片段 这个片段将会替换布局文件的 contentHtml\n\u003cdiv\u003e{{ age }}\u003c/div\u003e\n```\n\n#### 布局文件补充\n\n由上面代码可见，布局文件默认放到了 app/views 目录中，并起名 layout.html ，但这只是系统的默认配置，可以通过修改类的 layout 属性来改变布局文件的名称及位置\n\n```\nmodule.exports = class IndexController extends Controller {\n    run(req, res) {\n        this.getView().title = 'layout demo';\n        this.getView().enableLayout = true;\n\n        // 这里的 'app' 是一个系统别名\n        // 表示将默认布局文件修改为 app/layout/default.html\n        this.getView().layout = 'app/layout/default';\n\n        this.render('index', {\n            age: 20\n        })\n    }\n}\n```\n\n#### 向 head 部分添加资源\n\n有时候项目需要引入一个外部 javascript 文件，并且这个 javascript 文件需要在页面加载时先运行，模板引擎针对这种情况提供了向 html 的 head 部分追加内容的功能\n\n下面在模板文件中调用 `addHeadAsset()` 函数，实现添加头部资源。布局文件中调用 `getHeadAssets()` 实现了输出资源到页面\n\n```\n// 模板片段文件注册了一个样式文件\n{{ $this.addHeadAsset '\u003clink href=\"outer.css\"\u003e' }}\n\n\u003cdiv\u003eother html content\u003c/div\u003e\n```\n\n```\n// 布局文件使用 getHeadAssets() 输出了所有注册的资源\n\u003c!doctype html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"utf-8\"\u003e\n    {{{ $this.getHeadAssets }}}\n\u003c/head\u003e\n\u003cbody\u003e\n    content\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n#### 处理输出\n\n从 `candyjs 4.5.4`, `@candyjs/template-hbs 0.1.4` 开始支持返回模板处理结果，使用 `output` 参数控制是否直接输出或者返回模板渲染结果\n\n```\nmodule.exports = class IndexController extends Controller {\n    run(req, res) {\n        this.getView().title = 'layout demo';\n        this.getView().enableLayout = true;\n\n        // set output parameter\n        this.getView().output = false;\n\n        this.render('index', {\n            age: 20\n        }).then((data) =\u003e {\n            // todo\n            res.end(data);\n        });\n    }\n}\n```\n\n## CHANGELOG\n\n+ 2021-10-13\n\n    * 1.0.0 此版本依赖 candyjs \u003e= 4.9.3\n\n+ 2020-11-12\n\n    * 0.1.7 布局文件路径去除 @ 符号，由 '@app/views/layout' 修改为 'app/views/layout'\n\n+ 2020-09-15\n\n    * 0.1.5 优化布局文件参数 去除 title 和 description\n\n+ 2020-09-15\n\n    * 0.1.3 增加引入资源功能\n\n+ 2020-08-10\n\n    * 0.1.1 渲染模板添加 `$parameters` 参数\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcandyframework%2Fcandyjs-template-hbs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcandyframework%2Fcandyjs-template-hbs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcandyframework%2Fcandyjs-template-hbs/lists"}