1.0.0 • Published 10 months ago

hbs-to-html v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

hbs-to-html

Converts a handlebars file with partials or(and) layout to a html file.

Hypothesis

I 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 setting up a server e.g(expressjs) basicallyjust because i need to use handlebars as html with nodemailer.

Apart from that most handlebars packages relies on a web server to work, some implementation details are not clear enough lol.

So I need to build a simple package that helps me achieve my goal and offers the same capability as express-handlebars, but standalone, doesn't rely on a web server engine to work. IT JUST WORKS.

Installation

npm install hbs-to-html
or
yarn add hbs-to-html

Usage

Configuration

The package can be initialized with several options:

const HbsToHtml = require("hbs-to-html");

const templateConverter = new HbsToHtml({
  templateDirPath: "path/to/views",
});
OptionsRequiredDescription
templateDirPathyesDirectory path to where the template files are located.
defaultLayoutFilePathnoThe file path of the default layout file including the file extention e.g ('./layouts/main.hbs').
partialDirPathnoDirectory path to where the partial files are located.
extNamenoThe handlebars file extention being use in your code. Defaults to "hbs" if not provided.

You 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 😀.

The following example initialize the package with the all options providing the partials directory, the layout file, and an extName for of the handlebars files.

const templateConverter = new HbsToHtml({
  templateDirPath: "path/to/views",
  defaultLayoutFilePath: "path/to/layouts/main.hbs",
  partialDirPath: "path/to/partials",
  extName: "hbs"
});

Generating the html code

Use the compileToHtml() 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.

const generatedHtml = await templateConverter.compileToHtml({
  templateName: "filename",
  context: { 
    //any data you want to pass into the template(s) 
  }
});
OptionsRequiredDescription
templateNameyesName of the handlebars template file
contextnoAn object containing data you want to inject into the template(s)

Example

The template file

./views/index.hbs

  <p>This is a test paragraph.</p>

Note Please make sure your layout file includes the {{{body}}} within your body tag to inject the template else your template won't show.

The layout file

./views/layouts/main.hbs

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>{{title}}</title>
  <style>
    p {
      color: tomato;
    }
  </style>
</head>
<body>
  {{> header}}
    {{{body}}}
  {{> footer}}
</body>
</html>

Header partial file

./views/partials/header.hbs

<h1>This a test header.</h1>

Footer partial file

./views/partials/footer.hbs

<h1>Copyright &copy; sdk package Inc {{year}}</h1>
const path = require("path");
const HbsToHtml = require("hbs-to-html");

const templateConverter = new HbsToHtml({
  templateDirPath: path.join(__dirname, "views"), //required
  defaultLayoutFilePath: path.join(__dirname, "views", "layouts", "main.hbs"),
  partialDirPath: path.join(__dirname, "views", "partials"),
  extName: "hbs"
});

async function generateHtml(filename) {
  const generatedHtml = await templateConverter.compileToHtml({
    templateName: filename,
    context: { //any data you want to pass into the template(s)
      year: new Date().getFullYear(),
      title: "Testing package"
    }
  });

  return generatedHtml; 
}

generateHtml("index"); //would return a compile html code with all the template data inserted

I tested the code using http server, i generated the html code and passed the output to be rendered to the client. Screenshot below:

Screen shot of the above implementation result

Check out the example code implementation here.

How can I thank you?

Star the Github repo. I'd love the attention! Why not share the link for this repository on Twitter, Thread or HackerNews? Spread the word📢!

Don't forget to follow me on Twitter and on Thread. Let's chat.

Issues

Please use the issues tab on Github to create issues you encountered or feature you would love to see included in this awesome package.

Thank You!

Please freely reach out to me on the above channels. Thanks for using my package.

License

hbs-to-html is released under the MIT license.