smarthbs/readme.md

129 lines
5.7 KiB
Markdown
Raw Normal View History

2024-04-14 15:41:26 +00:00
# @push.rocks/smarthbs
2022-07-24 13:31:05 +00:00
handlebars with better fs support
2024-04-14 15:41:26 +00:00
## Install
To install `@push.rocks/smarthbs`, run the following command in your terminal:
```bash
npm install @push.rocks/smarthbs --save
```
This will add `@push.rocks/smarthbs` to your project's dependencies.
2022-07-24 13:31:05 +00:00
## Usage
2024-04-14 15:41:26 +00:00
The `@push.rocks/smarthbs` package enhances Handlebars with improved filesystem support, making it easy to manage partials and compile directories with template files. Below is a comprehensive guide to utilizing its capabilities in your project.
### Getting Started
First, ensure you've imported `@push.rocks/smarthbs` using ECMAScript Module (ESM) syntax in TypeScript:
```typescript
import * as smarthbs from '@push.rocks/smarthbs';
```
### Registering Partials
In Handlebars, *partials* are reusable templates that can be included within other templates. `@push.rocks/smarthbs` simplifies the process of registering all partials from a directory, enabling a more organized template structure.
To register all `.hbs` files from a directory (and its subdirectories) as partials:
```typescript
await smarthbs.registerPartialDir(pathToPartialsDirectory);
```
**Example:**
```typescript
await smarthbs.registerPartialDir('./views/partials');
```
This automatically registers each `.hbs` file in the directory as a partial that can be referenced by its path relative to the specified directory.
### Compiling Templates
`@push.rocks/smarthbs` allows you to compile an entire directory of Handlebars template files, outputting the rendered HTML to a specified directory. You can also provide a `.json` file containing data to be used by all templates during compilation.
**Example:**
```typescript
await smarthbs.compileDirectory(sourceDirectory, destinationDirectory, 'data.json');
```
This reads all `.hbs` files in `sourceDirectory`, compiles them using the data from `data.json`, and writes the resulting HTML to `destinationDirectory`.
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
### Finding Variables in Templates
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
When working with complex templates, it might be useful to programmatically find all variables used within a template:
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
```typescript
let varsInTemplate = await smarthbs.findVarsInHbsString(templateString);
console.log(varsInTemplate); // Outputs an array of variable names used in the template
2022-07-24 13:31:05 +00:00
```
2024-04-14 15:41:26 +00:00
### Checking Variables Satisfaction
To ensure that the data provided to a template includes all necessary variables, you can compare a template against a data object:
```typescript
let missingVars = await smarthbs.checkVarsSatisfaction(templateString, dataObject);
if(missingVars.length > 0) {
console.error('Some required variables are missing:', missingVars);
}
```
This function returns an array of missing variable names, allowing you to validate data completeness before rendering.
### Templates and Strings
You can also get templates directly from strings or files on disk, which can then be rendered with context data:
```typescript
// From a string
let templateFromString = await smarthbs.getTemplateForString("Hello {{name}}!");
let resultString = templateFromString({ name: "World" });
console.log(resultString); // Outputs: Hello World!
// From a file
let templateFromFile = await smarthbs.getTemplateForFile("./templates/greeting.hbs");
let resultFileString = templateFromFile({ name: "File World" });
console.log(resultFileString); // Outputs the rendered content of greeting.hbs with provided data
```
### Post-processing: Safe Syntax
In scenarios where Handlebars syntax needs to be preserved during an intermediate step:
```typescript
let safeString = await smarthbs.postprocess(yourTemplateString);
```
This method converts safe syntax markers (e.g., `{-{` and `}-}`) back into standard Handlebars syntax (`{{` and `}}`), useful if your templates go through multiple processing steps.
### Advanced Usage and Helpers
`@push.rocks/smarthbs` also supports advanced use cases and custom helpers. For example, registering a helper to log all available partials or to perform runtime template compilation based on dynamic data.
### Conclusion
`@push.rocks/smarthbs` provides a robust set of features to enhance your Handlebars templating, making it easier to manage and render complex templates with external data sources and organized partials. Whether you're building web applications, generating email templates, or any task involving templating, `@push.rocks/smarthbs` can simplify and streamline your workflow.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
### Company Information
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2022-07-24 13:31:05 +00:00
2024-04-14 15:41:26 +00:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.