1. html-template-tag
ES6 Tagged Template for compiling HTML template strings.
html-template-tag
Package: html-template-tag
Created by: AntonioVdlC
Last modified: Sat, 18 Jun 2022 21:57:40 GMT
Version: 4.0.1
License: MIT
Downloads: 11,014
Repository: https://github.com/AntonioVdlC/html-template-tag

Install

npm install html-template-tag
yarn add html-template-tag

html-template-tag

version
issues
downloads
license

ES6 Tagged Template for compiling HTML template strings.

Installation

This package is distributed via npm:

npm install html-template-tag

Usage

String Interpolation

At its core, this module just performs simple ES6 string interpolation.

 var html = require("html-template-tag");
// - or - import { html } from "html-template-tag";

var name = `Antonio`;
var string = html`Hello, ${name}!`;
// "Hello, Antonio!"

Nevertheless, it escapes HTML special characters without refraining its use in loops!

 var html = require("html-template-tag");
// - or - import { html } from "html-template-tag";

var names = ["Antonio", "Megan", "/><script>alert('xss')</script>"];
var string = html`
	<ul>
		${names.map((name) => html`
			<li>Hello, ${name}!</li>
		`)}
	</ul>
`;
// "<ul><li>Hello, Antonio!</li><li>Hello, Megan!</li><li>Hello, /&gt;&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;!</li></ul>"

Skip autoscaping

You can use double dollar signs in interpolation to mark the value as safe (which means that this variable will not be escaped).

 var name = `<strong>Antonio</strong>`;
var string = html`Hello, $${name}!`;
// "Hello, <strong>Antonio</strong>!"

HTML Template Pre-Compiling

This small module can also be used to pre-compile HTML templates:

 var html = require("html-template-tag");
// - or - import { html } from "html-template-tag";

var data = {
	count: 2,
	names: ["Antonio", "Megan"]
};

var template = ({names}) => html`
	<ul>
		${names.map((name) => html`
			<li>Hello, ${name}!</li>
		`)}
	</ul>
`;

var string = template(data);
/* 
	"
	<ul>
		<li>Hello, Antonio!</li>
		<li>Hello, Megan!</li>
	</ul>
	"
*/

NB: The formating of the string literal is kept.

License

MIT

Thanks

The code for this module has been heavily inspired on Axel Rauschmayer's post on HTML templating with ES6 template strings and Stefan Bieschewski's comment.

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin