Skip to content
velocity for js
Branch: master
Clone or download
shepherdwind Merge pull request #115 from JoaoSouMoreira/master
Added support for subList in arrays
Latest commit 1fa2b45 Oct 16, 2018

Velocity - Template Engine

NPM version build status Test coverage npm download

Velocityjs is velocity template engine for javascript.



  • 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.


via npm:

$ npm install velocityjs


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
  3. Object.keys


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);



  • 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 is an object or undefined, for vm $, data look up path will be 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);



  • asts {array} array of vm asts tree
  • config {object} you can define some option for Compile
  • escape {boolean} default true, default escape variable to html encode, you can set false to close 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



  • 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 you can find from velocity user guide


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


You can find help from those ways:

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


Recommend an other velocity.


(The MIT License)

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.