Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

velocityjs

shepherdwind1.4mMIT2.0.6TypeScript support: included

Velocity Template Language(VTL) for JavaScript

velocity template

readme

Velocity - Template Engine

NPM version build status Test coverage npm download

Velocityjs is velocity template engine for javascript.

中文版文档

Features

  • Supports both client and server side use.
  • Separation of parsing and rendering templates.
  • The basic syntax is fully supported all java version velocity.
  • Vim Syntax for vim.

Install

via npm:

$ npm install velocityjs

Browser

Compatible with all modern browsers. You can try test case in your browser to test it.

For other lower version browsers, you need have those polyfill function.

  1. Array.prototype map, forEach, some, filter, every, indexOf
  2. Date.now
  3. Object.keys

Examples

You can find a lot of examples from the tests directory. There is no different between the use of browser and NodeJs.

Public API

{
  // render method
  render(vm: string, context?: Object, macros?: Object): string;

  parse(vm: string, config?: Object, ignorespace?: boolean): Array<Ast>;

  Compile: {
    (asts: Array<Ast>, config?: Object): {
      render(context?: Object, macros?: Object);
    };
  };
}

render

params:

  • vm {string} velocity string input
  • context {object} render context, data or function for vm
  • macros {object} such as #include('path/xxx') , you can define you inlcude macro function
var Velocity = require('velocityjs');

Velocity.render('string of velocity', context, macros);

context

context is an object or undefined, for vm $foo.bar, data look up path will be context.foo.bar. context can have method, and call it just on velocity string.

The method of context, will have eval method on this of inner method body. You can eval to rerender velocity string, such as test code $control.setTemplate.

Compile and parse

parse method can parse vm, and return ast tree of velocity.

Compile will render asts to result string.

var Compile = Velocity.Compile;

var asts = Velocity.parse('string of velocity');
(new Compile(asts)).render(context, macros);

Compile

params:

  • asts {array} array of vm asts tree
  • config {object} you can define some option for Compile
config
  • escape {boolean} default false, default not escape variable to html encode, you can set true to open it.
  • unescape {object} define the object, which key do not need escape. For example, set unescape equal {control: true}, so $control.html will not escape.
  • env {string} when env equal development will throw error when null values are used
  • valueMapper {function} this config allow us to redefine the #set value, @see https://github.com/shepherdwind/velocity.js/pull/105
  • customMethodHandlers {Array} Used to implement some custom function behavior, the specific use of reference https://github.com/shepherdwind/velocity.js/issues/145

parse

params:

  • vm {string} string to parse
  • blocks {object} self define blocks, such as #cms(1) hello #end, you can set {cms: true}
  • ignorespace {boolean} if set true, then ignore the newline trim.

Syntax

Syntax you can find from velocity user guide

Directives

Directives supports have set, foreach, if|else|elseif, macro, break, stop.

Some othe directive evaluate, define, parse, do not supported default, but You can realize by context or macros, for example parse

Questions

You can find help from those ways:

  1. New issue
  2. Email to eward.song at gmail.com
  3. 阿里内部员工,可以通过 hanwen.sah 搜到我的旺旺

Other

Recommend an other velocity.

License

(The MIT License)

changelog

Changelog

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

v2.0.6

  • 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