包详细信息

velocityjs

shepherdwind1.8mMIT2.1.5

Velocity Template Language(VTL) for JavaScript

velocity template

自述文件

Velocity.js

NPM version build status npm download

A JavaScript implementation of the Apache Velocity template engine.

Features

  • ✨ Full support for both client-side and server-side rendering
  • 🔄 Separation of template parsing and rendering phases
  • 🎯 Complete compatibility with Java Velocity syntax
  • 🚀 High performance and lightweight

Installation

npm install velocityjs

Quick Start

import { render, parse, Compile } from 'velocityjs';

// Simple rendering
const result = render('Hello $name!', { name: 'World' });
console.log(result); // Output: Hello World!

// With macros
const macros = {
  include: (path) => `Included content from ${path}`,
};
const template = '#include("header.vm") Hello $name!';
const rendered = render(template, { name: 'World' }, macros);

API Reference

Core Methods

render(vm, context, macros)

Renders a Velocity template string with the provided context and macros.

Parameters:

  • vm (string) - Velocity template string
  • context (object) - Data context for template variables
  • macros (object) - Custom macro functions

Returns: (string) Rendered output

parse(vm, config, ignorespace)

Parses a Velocity template into an AST (Abstract Syntax Tree).

Parameters:

  • vm (string) - Velocity template string
  • config (object) - Parsing configuration
  • ignorespace (boolean) - Whether to ignore whitespace trimming

Returns: (Array) AST nodes

Compile

Compiles parsed AST into a renderable template.

import { parse, Compile } from 'velocityjs';

const asts = parse('Hello $name!');
const template = new Compile(asts);
const result = template.render({ name: 'World' });

Configuration Options

Compile Configuration

  • escape (boolean) - Enable HTML escaping for variables (default: false)
  • unescape (object) - Specify variables to exclude from escaping
  • env (string) - Set to 'development' to throw errors on null values
  • valueMapper (function) - Custom value transformation for #set directives
  • customMethodHandlers (Array) - Custom function behavior implementations

Context and Macros

Context Object

The context object provides data and methods to your templates:

  • Properties are accessed using dot notation: $user.name
  • Methods can be called directly: $formatDate($date)
  • Methods have access to an eval method for dynamic rendering

Macros

Custom macro functions can be defined for directives like #include:

import { render } from 'velocityjs';

const macros = {
  include: (path) => {
    // Custom include implementation
    return readFile(path);
  },
};

Supported Directives

  • #set - Variable assignment
  • #foreach - Loop iteration
  • #if/#else/#elseif - Conditional logic
  • #macro - Template macro definition
  • #break - Loop control
  • #stop - Template execution control

Getting Help

License

MIT License

更新日志

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

v2.1.5

  • chore: update type definitions path in package.json to point to src directory 52c651a

v2.1.4

23 April 2025

  • chore: update type definitions path in package.json and remove install script from package-lock.json 6b44cbb

v2.1.3

14 April 2025

  • feat: add default export for velocity object #169
  • chore: remove Chinese README and update English documentation dcc646e

v2.1.2

14 April 2025

  • chore: update package-lock.json to use npm registry cbeaab3
  • chore: update dependencies in package.json and package-lock.json 456e6d2
  • chore: modify publish script in package.json 0f7fa8f

v2.1.1

13 April 2025

  • chore: update dependencies c299117
  • chore: remove package-lock.json and update README and VSCode settings 2cdd37f

v2.1.0

13 April 2025

  • TypeScript support #158
  • chore(deps-dev): bump express from 4.18.2 to 4.19.2 #163
  • chore(deps-dev): bump @babel/traverse from 7.16.8 to 7.24.1 #162
  • chore(deps-dev): bump ip from 1.1.5 to 1.1.9 #161
  • chore(deps): bump xml2js and aws-sdk #159
  • chore(deps): bump async from 2.6.3 to 2.6.4 #150
  • chore(deps): bump minimatch from 3.0.4 to 3.1.2 #152
  • chore(deps): bump qs and express #154
  • chore(deps): bump moment from 2.29.2 to 2.29.4 #157
  • chore(deps): bump express from 4.17.2 to 4.18.2 #155
  • chore(deps): bump json5 from 2.2.0 to 2.2.3 #156
  • chore(deps): bump node-fetch from 2.6.6 to 2.6.7 #148
  • chore(deps): bump moment from 2.29.1 to 2.29.2 #149
  • chore(deps): bump minimist from 1.2.5 to 1.2.6 #147
  • fix: restore project to stable state, remove ESM configuration, fix build and tests fcbbf37
  • chore: update package.json and GitHub Actions for Node.js version compatibility 9d3840a
  • feat: upgrade all test and source code to typescript b749a27

v2.0.6

16 January 2022

  • feat: add custom method handlers support, refactor references.js #146
  • chore: add npm ci support 91baab1
  • chore: update readme.md eb8c93a

v2.0.5

12 November 2021

  • chore: better error handler #142
  • Addressing #140, also added a bit more defensive code in case pos is missing in error situations #141
  • Fixing #140, also added a bit more defensive code in case pos is missing in error situations eed1a16
  • chore: update history script 585bdab

v2.0.4

27 October 2021

  • fix: support null as var, fix #139 #139
  • chore: support auto changelog 984fc38

v2.0.3

2 December 2020

  • fix: issue with loop variable access inside nested foreach statements #136
  • fix: issue with loop variable access inside nested foreach statements #135
  • chore: 2.0.3 93245a5

2.0.3 2020-12-02

  • fix: issue with loop variable access inside nested foreach statements 136

2.0.2 2020-10-19

  • add default set and get method for array and map object 133

2.0.1 2020-09-02

  • fix Bug with #set inside #foreach 129

2.0.0 2019-09-26

Break change

First, default escape config set to false. For example

#set($foo = '<div>') $foo

At 1.x , result is &lt;div&gt;, for 2.x, output result <div>, no escapeHtml variable.

  • add quotes to escape when escape option set true 125

1.1.4 2019-08-27

  • Added support for remove in ArrayList and Map @mdrewt #124
  • Support format quiet syntax ${ ref }, fix #120

1.1.3 2018-09-18

  • fixes issue#113 support for add method on arrays by @gauravlanjekar #114

1.1.2 2018-08-09

  • fix Velocity cli error fix #109

1.1.1 2018-06-14

  • fix: Allow own keySet, entrySet, put and size methods on objects by @lightsofapollo !106

1.1.0 2018-06-12

  • feat: Add support for .put, fix issue #103 by @lightsofapollo
  • feat: add config.valueMapper support by @lightsofapollo #105

1.0.2 2018-06-06

  • fix: ignore path where build on broswer, fix issue #102

1.0.1 2018-03-08

  • fix: #set bug in nested #foreach loops #100

1.0.0 2018-01-13

  • feat: support macro bodyContext !97

0.10.1 2017-11-10

  • fix: #set variable key not work !91

0.10.0 2017-11-08

  • feat: support text version of logical operators !90

0.9.6 2017-04-14

  • fix: keep newline after unparse block !83

0.9.5 2017-04-07

  • fix: support foreach.hasnext when iterating objects !81

0.9.4 2017-01-16

  • fix: #set bug with eval string #79

0.9.3 2017-01-04

  • fix: #set false when in forEach statement #77

0.9.1 2016-11-22

  • add typescript DefinitelyTyped index.d.ts

0.9.0 2016-09-20

  • feat: support to throw errors in case null values are used !71
  • fix: support {end} !70

0.8.5 2016-07-16

  • fix bug #foreach with nest empty foreach !70

0.8.4 2016-07-04

  • fix bug when render #foreach(${itemData} in ${defaultData}), see #69

0.8.3 2016-06-15

  • fix comment bug #67

0.8.2 2016-05-28

  • nothing change, just update npm page readme

0.8.0 2016-04-20

  • set bug fix #63

Break api change

Before set value will set undefined node as plan object

#set($a = {}) #set($a.c.d = 1) $a.c.d

This will output 1 .

But now, in 0.8.0 version, a.c will not set as {}, so output is $a.c.d.

0.7.5

  • toString rewrite only when toString equal function { [native code]} 57

0.7.4

  • toString like org.apache.velocity when render 56

0.7.3

  • fix 53
  • runt support 54

0.7.2

  • fix: merge 52

0.7.1

  • fix: merge 51

0.7.0

0.6.2

  • feat: merge 47

0.6.1

  • feat: merge 46

0.6

  • change: remove Velocity.Parser, change to Velocity.parse #43
  • feat: add custom blocks support #44

0.4

0.4.11 / 2015-01-24

  • feat: self define macro context keep to origin object

0.4.10 / 2015-01-08

  • fix: allow optional space after colon in map passed as parameter to macro (#38 by @jamescookie)

0.4.9 / 2014-12-29

  • feature: support friendly error stack #35
  • chore: improve coverage

0.4.8 / 2014-12-20

  • fix issue #32
  • Remove useless code: Helper
  • merge pull request #34

0.4.7 / 2014-12-18

  • fix issue #32