npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mojojs/template

v2.2.7

Published

Very fast embedded JavaScript templates

Downloads

3,147

Readme

Coverage Status npm

A very fast embedded JavaScript template engine for server-side rendering (SSR) in Node.js. Written in TypeScript.

import Template from '@mojojs/template';

// One-off
const result = await Template.render('Hello <%= name %>!', {name: 'World'});

// Compile a function for reuse
const template = new Template('Hello <%= name %>!');
const fn = template.compile();
const result = await fn({name: 'World'});

// Use a custom escape function and name
const escape = function (input) { return `"${input}"` };
const template = new Template('Date: <%= date %>', {escape, name: 'now.tmpl'});
const fn = template.compile();
const result = await fn({date: new Date()});

For easier syntax highlighting of templates embedded in JavaScript and TypeScript code, there is also an tmpl tagged template literal available.

import {tmpl} from '@mojojs/template';

const template = tmpl`Hello <%= name %>!`;
const fn = template.compile();
const result = await fn({name: 'World'});

This module is designed specifically for all the small tasks that come up during big projects, like preprocessing config files. Not just to generate HTML or XML documents in web applications.

Syntax

All templates are compiled to async functions, so you can safely use await.

<% JavaScript code %>
<%= JavaScript expression, replaced with XML escaped result %>
<%== JavaScript expression, replaced with result %>
<%# Comment, useful for debugging %>
<%% Replaced with "<%", useful for generating templates %>
% JavaScript code line, treated as "<% line =%>" (explained later)
%= JavaScript expression line, treated as "<%= line %>"
%== JavaScript expression line, treated as "<%== line %>"
%# Comment line, useful for debugging
%% Replaced with "%", useful for generating templates

JavaScript lines can be indented freely.

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    %== metaTags
  </head>
  <body>
    %= content
  </body>
</html

Expressions and code blocks can also be split up over multiple lines.

<div><%= 'Hello '
         + randomName + '!' %></div>

Whitespace characters around tags can be trimmed by adding an additional equal sign to the end of a tag.

<% for (let i = 1; i <= 3; i++) { =%>
  <%= 'The code blocks around this expression are not visible in the output' %>
<% } =%>

Code lines are automatically trimmed and always completely invisible in the output.

% for (let i = 1; i <= 3; i++) {
  <%= 'The code lines around this expression are not visible in the output' %>
% }

Template Blocks

You can also capture whole template blocks as async functions for reuse later with <{blockName}> and <{/blockName}> tags. Similar to code lines, these tags are automatically trimmed and invisible in the output. The use of named parameters is optional.

<{helloBlock(name)}>
  Hello <%= name %>.
<{/helloBlock}>
<%= await helloBlock('Baerbel') %>
<%= await helloBlock('Wolfgang') %>

To generate template blocks you can use <{{blockName}}> and <{{/blockName}}> tags.

Debugging

To help with debugging, all thrown exceptions as well as syntax errors are expanded with context information whenever possible. This does not incur a performance penalty, and therfore does not need to be disabled in production.

Error: template:7
    5| </div>
    6| <div>
 >> 7| % throw new Error('Something went wrong...');
    8| <main>
    9|
Something went wrong...
    at eval (eval at compile (file:///home/kraih/repo/template.js/src/template.ts:53:18), <anonymous>:9:8)
    at file:///home/kraih/repo/template.js/src/template.ts:55:19
    at Function.render (file:///home/kraih/repo/template.js/src/template.ts:64:51)
    at Test.<anonymous> (file:///home/kraih/repo/template.js/test/template.js:191:24)
    at TapWrap.runInAsyncScope (node:async_hooks:199:9)
    ...

The generated code for template functions is usually quite efficient, but to avoid possible syntax errors it's good to be aware of its general structure. Especially with regard to where the use of semicolons is required.

<% const message = 'World'; %>
Hello
<%= message %>!
try { with(__locals){ let __output = ''; const message = 'World';
__output += 'Hello\n';
__output += __escape(message); __output += '!\n'; return __output; } } catch (error) { __context(error, __source) }

You can set the MOJO_TEMPLATE_DEBUG=1 environment variable to get the generated code printed to stderr.

Editor Support

Installation

All you need is Node.js 16.0.0 (or newer).

$ npm install @mojojs/template

Support

If you have any questions the documentation might not yet answer, don't hesitate to ask in the Forum, on Matrix, or IRC.