Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zizwar/jhtm

JavaScript HTML Template Manager for browser and Node.js
https://github.com/zizwar/jhtm

browser html javascript json jsx nodejs template template-manager vanilla-javascript

Last synced: 5 days ago
JSON representation

JavaScript HTML Template Manager for browser and Node.js

Awesome Lists containing this project

README

        

# JHTM (JavaScript HTML Template Manager)

JHTM is a lightweight yet powerful library for managing HTML templates using JavaScript. It can be used in both browser and Node.js environments.

## Features

- Support for browser and Node.js usage
- Load templates from URL or local file
- Support for multiple data sources (JSON URL, JavaScript object)
- Template caching for improved performance
- Support for dynamic code execution within templates
- Proper handling of script tags, CSS files, and other external resources

## Installation

```bash
npm install jhtm
```

## Usage

### In Browser

```html

const jhtm = new JHTM('/template.html', '/data.json'/*or "https://exemple.com/feed.json"*/, { cacheTemplate: true });
jhtm.render().then(result => {
document.body.innerHTML = result;
});

```

### In Node.js

```javascript
const JHTM = require('jhtm');

const jhtm = new JHTM('/template.html', { name: 'John', age: 30 });
jhtm.render().then(result => {
console.log(result);
});
```

## Options

You can pass a configuration object as the third argument to the constructor:

```javascript
const config = {
cacheTemplate: true, // Enable template caching
cacheTTL: 3600000, // Cache time-to-live in milliseconds (default 1 hour)
executeScripts: true, // Execute inline scripts and load external scripts
loadCSS: true // Load external CSS files
};

const jhtm = new JHTM('/template.html', '/data.json', config);
```

## Template Example

```html

{{name}}


Age: {{age}}



${data.hobbies.map(hobby => `${hobby}`).join(', ')}

console.log('Hello from template!');

```

## License

MIT