java velocity js_velocityjs

Velocity Template Language(VTL) for JavaScript

Velocity - Template Engine

velocityjs.svg?style=flat-square

master.svg?style=flat-square

velocity.js.svg?style=flat-square

velocityjs.svg?style=flat-square

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.

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.

Array.prototype map, forEach, some, filter, every, indexOf

Date.now

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;

Compile: {

(asts: Array, 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

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:

Email to eward.song at gmail.com

阿里内部员工,可以通过 hanwen.sah 搜到我的旺旺

Other

Recommend an other velocity.

License

(The MIT License)

HomePage

Repository

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值