{"id":15091669,"url":"https://github.com/biggaji/handlebars-converter","last_synced_at":"2026-01-04T15:46:52.552Z","repository":{"id":194359898,"uuid":"690536456","full_name":"biggaji/handlebars-converter","owner":"biggaji","description":"Converts handlebars code with partials or(and) layout to html","archived":false,"fork":false,"pushed_at":"2023-09-12T23:41:30.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-03-15T05:20:15.035Z","etag":null,"topics":["biggaji","handlebars","handlebars-converter","handlebars-js","handlebarsjs","oxwware"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/handlebars-converter","language":"JavaScript","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/biggaji.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,"governance":null}},"created_at":"2023-09-12T11:44:32.000Z","updated_at":"2023-11-24T21:30:53.000Z","dependencies_parsed_at":"2023-09-13T02:09:07.938Z","dependency_job_id":null,"html_url":"https://github.com/biggaji/handlebars-converter","commit_stats":null,"previous_names":["biggaji/handlebars-converter"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggaji%2Fhandlebars-converter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggaji%2Fhandlebars-converter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggaji%2Fhandlebars-converter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/biggaji%2Fhandlebars-converter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/biggaji","download_url":"https://codeload.github.com/biggaji/handlebars-converter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244952189,"owners_count":20537463,"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":["biggaji","handlebars","handlebars-converter","handlebars-js","handlebarsjs","oxwware"],"created_at":"2024-09-25T10:42:36.668Z","updated_at":"2026-01-04T15:46:52.525Z","avatar_url":"https://github.com/biggaji.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# handlebars-converter\nConverts handlebars code with partials or(and) layout to html.\n\n# Hypothesis\nI built this package specfically because I want an easier way to generate or compile a handlebars file into html code that I could pass to my `nodemailer` html property without having to set up a server e.g(`expressjs`) basically just because I need to use handlebars as html templating engine with nodemailer.\n\nApart from that most handlebars packages relies on a web server to work, some implementation details are not clear enough lol.\n\nSo I need to build a simple package that helps me achieve my goal and offers the same capability as `express-handlebars`, but standalone, does not rely on a web server to work. IT JUST WORKS.\n\n# Installation\n\n```sh\nnpm install handlebars-converter\nor\nyarn add handlebars-converter\n```\n\n# Usage\n\n## Configuration\n\nThe package can be initialized with several options:\n\n```js\nconst { HandlebarsConverter } = require(\"handlebars-converter\");\n\nconst templateConverter = new HandlebarsConverter({\n  templateDirPath: \"path/to/views\",\n});\n```\n\n| Options   | Required | Description                                                  |\n| --------------- | -------- | ----------------------------- |\n| `templateDirPath`   | yes      | Directory path to where the template files are located. |\n| `defaultLayoutFilePath` | no | The file path of the default layout file including the file extention e.g (`'./layouts/main.hbs'`). |\n| `partialDirPath` | no | Directory path to where the partial files are located.|\n| `extName` | no | The handlebars file extention being use in your code. Defaults to `\"hbs\"` if not provided. |\n\nYou can initialize the package accordingly based on what options you have i.e are there partial files? Add the `partialDirPath` option to the constructor and provide the directory path as it value, do you have a default layout? Add the `defaultLayoutFilePath` option and provide the file path as a value to it. The optional fields work independent of each other. You provide what you have and you get an output of what you provide 😀. \n\n\nThe following example initialize the package with the all options providing the  `partials` directory, the `layout` file, and an `extName` for of the handlebars files.\n\n```js\nconst templateConverter = new HandlebarsConverter({\n  templateDirPath: \"path/to/views\",\n  defaultLayoutFilePath: \"path/to/layouts/main.hbs\",\n  partialDirPath: \"path/to/partials\",\n  extName: \"hbs\",\n});\n```\n\n## Generating the html code\nUse the `compile()` method available on the templateConverter class instance created earlier to generate an html output, it returns a promise so we need to use await keyword.\n\n```js\nconst generatedHtml = await templateConverter.compile({\n  templateName: \"filename\",\n\n  // any data you want to pass into the template(s)\n  context: {},\n});\n```\n\n| Options   | Required | Description                                                  |\n| --------------- | -------- | ----------------------------- |\n| `templateName`   | yes      | Name of the handlebars template file `e.g(index.hbs)`, `index` is the templateName |\n| `context` | no | An object containing data you want to inject into the template(s) |\n\n\n\n# Example\n\nThe template file\n\n./views/index.hbs\n```hbs\n  \u003cp\u003eThis is a test paragraph.\u003c/p\u003e\n```\n\n\n\u003e **Note** Please make sure your layout file includes the `{{{body}}}` within your body tag to inject the template else your template won't show.\n\nThe layout file\n\n./views/layouts/main.hbs\n```hbs\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n  \u003cmeta charset=\"UTF-8\"\u003e\n  \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n  \u003ctitle\u003e{{title}}\u003c/title\u003e\n  \u003cstyle\u003e\n    p {\n      color: tomato;\n    }\n  \u003c/style\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n  {{\u003e header}}\n    {{{body}}}\n  {{\u003e footer}}\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nHeader partial file\n\n./views/partials/header.hbs\n```hbs\n\u003ch1\u003eThis a test header.\u003c/h1\u003e\n```\n\nFooter partial file\n\n./views/partials/footer.hbs\n```hbs\n\u003ch1\u003eCopyright \u0026copy; sdk package Inc {{year}}\u003c/h1\u003e\n```\n\n```js\nconst path = require(\"path\");\nconst { HandlebarsConverter } = require(\"handlebars-converter\");\n\nconst templateConverter = new HandlebarsConverter({\n  templateDirPath: path.join(__dirname, \"views\"), //required\n  defaultLayoutFilePath: path.join(__dirname, \"views\", \"layouts\", \"main.hbs\"),\n  partialDirPath: path.join(__dirname, \"views\", \"partials\"),\n  extName: \"hbs\"\n});\n\nasync function generateHtml(filename) {\n  const generatedHtml = await templateConverter.compile({\n    templateName: filename,\n\n    //any data you want to pass into the template(s)\n    context: {\n      year: new Date().getFullYear(),\n      title: \"Testing package\"\n    }\n  });\n\n  return generatedHtml; \n}\n\n// would return a compiled html code with all the template data injected\ngenerateHtml(\"index\"); \n```\n\nI tested the code using http server, i generated the html code and passed the output to be rendered to the client. Screenshot below:\n\n![Screen shot of the above implementation result](https://res.cloudinary.com/dahn8uiyc/image/upload/v1688652447/hbs-to-html-snipshot_lug4m3.png)\n\n\nCheck out the example [code implementation here](https://github.com/biggaji/handlebars-converter/tests).\n\n\n## How can I thank you?\n\nStar the [Github repo](https://github.com/biggaji/handlebars-converter). I'd love the attention! Why not share the link for this repository on Twitter, Thread or HackerNews? Spread the word📢!\n\nDon't forget to follow me on [Twitter](https://twitter.com/oxwware) and on [Thread](https://threads.net/@oxwware). Let's chat.\n\n# Issues\nPlease use the [issues tab](https://github.com/biggaji/handlebars-converter/issues) on Github to create issues you encountered or feature you would love to see included in this awesome package.\n\n# Thank You!\nPlease freely reach out to me on the above channels.\nThanks for using my package.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiggaji%2Fhandlebars-converter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbiggaji%2Fhandlebars-converter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbiggaji%2Fhandlebars-converter/lists"}