Replace your template engine with fast JavaScript by leveraging the power of tagged templates.
Inspired by html-template-tag.
npm i ghtml
The main export of the package is the html
function that can be used to tag template literals and escape their expressions. To bypass escaping an expression, prefix it with !
.
Node.js users also have access to the includeFile
function that reads and outputs the content of a file while caching it in memory for future use.
import { html } from "ghtml";
const username = '<img src="https://example.com/hacker.png">';
const greeting = html`<h1>Hello, ${username}!</h1>`;
console.log(greeting);
// Output: <h1>Hello, <img src="https://example.com/hacker.png"></h1>
const img = '<img src="https://example.com/safe.png">';
const container = html`<div>!${img}</div>`;
console.log(container);
// Output: <div><img src="https://example.com/safe.png"></div>
The includeFile
function returns the content of a file. Again, remember that it also caches the result, so any subsequent modifications to the same file won't be reflected until the app is restarted:
import { includeFile } from "ghtml/includeFile.js";
const logo = includeFile("static/logo.svg");
console.log(logo);
// Output: content of "static/logo.svg"