pug-template-tag
v2.0.2
Published
Declare safe Pug templates inline in JavaScript
Downloads
3
Readme
Pug Template Tag
A template tag that allows defining safe Pug templates inline in JavaScript or TypeScript code.
Installation
$ npm install pug-template-tag
Usage
// Load the template tag.
const pug = require('pug-template-tag');
// Define a function using inline Pug code.
const templateFunction = pug({ /* pug options */ })`
doctype html
p
=text
`;
// Apply a template to get HTML.
const html = templateFunction({ text: 'Hello, World!' });
Alternatively, you can configure the template tag once and reuse it.
// Load the template tag.
const pugTemplateTag = require('pug-template-tag');
// Create a configured template tag.
const pug = pugTemplateTag({ /* pug options */ });
const templateFoo = pug`
doctype html
.foo= text`;
const templateBar = pug`
doctype html
.bar= text`;
API
Package pug-template-tag exports a function that may either be called as a template tag to define a template or as a function that takes an options bundle.
pug(pugOptions)
When called with a Pug options bundle, the exported function returns an instance of the same function but which uses the given options bundle.
pug...
When called as a template tag:
- Reuses a previously compiled version if available.
- Strip common leading whitespace so you can indent Pug templates to match the indentation of surrounding code.
- Compiles the Pug source to a function.
- Creates a CommonJS module for the template code under the namespace
@pug-template
. - Returns the template function.
Escaping
The pug template tag behaves like String.raw
.
Module System
The pug template functions makes sure that error trace will point to the source file.
Plugins
Regardless of options, the pug template function always attaches two plugins:
- A debug plugin that rewrites line numbers and files so that error messages point to the JavaScript code that defined the template.
- The Trusted Types plugin makes the template resistant to XSS since it is not a goal of this project to make it easier to produce unsafe templates.