Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/billykong/simple-templating-engine
A simple template engine which you can customise with a handler function
https://github.com/billykong/simple-templating-engine
js-template json-template template template-engine
Last synced: about 2 months ago
JSON representation
A simple template engine which you can customise with a handler function
- Host: GitHub
- URL: https://github.com/billykong/simple-templating-engine
- Owner: billykong
- License: mit
- Created: 2018-06-14T04:00:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T20:16:47.000Z (about 2 years ago)
- Last Synced: 2024-08-09T12:10:56.495Z (5 months ago)
- Topics: js-template, json-template, template, template-engine
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/simple-template-engine
- Size: 249 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/billykong/simple-templating-engine.svg?branch=master)](https://travis-ci.org/billykong/simple-templating-engine)
[![Coverage Status](https://coveralls.io/repos/github/billykong/simple-templating-engine/badge.svg?branch=master)](https://coveralls.io/github/billykong/simple-templating-engine?branch=master)
[![npm version](https://img.shields.io/npm/v/simple-template-engine)](https://img.shields.io/npm/v/simple-template-engine)
[![Run on Repl.it](https://repl.it/badge/github/billykong/simple-templating-engine)](https://repl.it/github/billykong/simple-templating-engine)# Simple Template Engine
## Usage
```JavaScript
var templeteEngine = require('./simple_templating_engine.js');
var template = "Hello, <% change_me %>";
var handlerWorld = function(key) { return { '<% change_me %>': 'World' } };templeteEngine.populate(template, handlerWorld).then(populated => {
console.log(populated); // "Hello, World"
});var handlerAsyncWorld = function(key) { return { '<% change_me %>': 'Async World' } };
(async function() {
let populated = await templeteEngine.populate(template, handlerAsyncWorld);
console.log(populated); // Hello, Async World
})();```
It also work with JSON template:
```JavaScript
var templateJSON = `{ "root": <% change_me %> }`;
var handlerJSON = function(key) {
return {
'<% change_me %>': {
'key1': 'value1',
'key2': 'value2'
}
}
};templeteEngine.populate(templateJSON, handlerJSON).then(populated => {
console.log(JSON.parse(populated));
// { "root": {"key1":"value1","key2":"value2"} }
});
```The handler can be an async function when we need to call remote APIs to get data:
```JavaScript
var handlerJSON = async function(key) {
return await {
'<% change_me %>': {
'key1': 'value1',
'key2': 'value2'
}
}
};
templeteEngine.populate(templateJSON, handlerJSON).then(populated => {
console.log(populated);
});
```You can also use another matcher pattern other than `<% ... %>`:
```JavaScript
var template = "Hello, {_ change_me _}";
var options = { matcher: /"{_([^%>]+)?_}"/g}
var handler = function(key) { return { '{_ change_me _}': 'World' } };templeteEngine.populate(template, handler, options).then(populated => {
console.log(populated); // "Hello, World"
});
```