pug-template-tag

2.0.2 • Public • Published

Pug Template Tag

npm

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:

  1. Reuses a previously compiled version if available.
  2. Strip common leading whitespace so you can indent Pug templates to match the indentation of surrounding code.
  3. Compiles the Pug source to a function.
  4. Creates a CommonJS module for the template code under the namespace @pug-template.
  5. 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:

  1. A debug plugin that rewrites line numbers and files so that error messages point to the JavaScript code that defined the template.
  2. 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.

Readme

Keywords

Package Sidebar

Install

npm i pug-template-tag

Weekly Downloads

3

Version

2.0.2

License

(MIT OR Apache-2.0)

Unpacked Size

10.4 kB

Total Files

4

Last publish

Collaborators

  • mikesamuel