hang-me
v1.0.1
Published
A simple zero dependencies Javascript tool that compile string template
Downloads
15
Maintainers
Readme
Getting Started
It's a simple zero dependencies template string compiler for Javascript and Typescript.
Basic Usage
import { Compiler } from "hang-me";
const template = "My name is: {{name}}!";
const compiler = new Compiler();
const result = compiler.compile(template, {
name: 'John Doe'
});
console.log(result); // my name is: John Doe!
Object and array
You can get values directly from embed object
import { Compiler } from "hang-me";
const template = "Hello, I am {{user.firstname}} {{user.lastname}}!";
const compiler = new Compiler();
const result = compiler.compile(template, {
user: {
firstname: 'John',
lastname: 'Doe'
}
});
console.log(result); // Hello, I am John Doe!
Or you can get value from array
import { Compiler } from "hang-me";
const template = "Hello, I am {{user.info[0]}} {{user.info[1]}}!";
const compiler = new Compiler();
const result = compiler.compile(template, {
user: {
info: [
'John',
'Doe'
]
}
});
console.log(result); // Hello, I am John Doe!
Functions
You can also call a function inside your template by using this syntax
import { Compiler } from "hang-me";
const template = "Hello, I am {{fullnameUpperCase: firstname lastname}}!";
const compiler = new Compiler();
const result = compiler.compile(template, {
firstname: 'John',
lastname: 'Doe',
fullnameUpperCase: (firstname: string, lastname: string) => {
return `${firstname} ${lastname}`.toUpperCase();
}
});
console.log(result); // Hello, I am JOHN DOE!
Javascript
You can also use require
const { Compiler } = require("hang-me");
const template = "My name is: {{name}}!";
const compiler = new Compiler();
const result = compiler.compile(template, {
name: 'John Doe'
});
console.log(result); // my name is: John Doe!