vtpl

1.1.2 • Public • Published

v npm package size

a tiny template engine

Installation

$ npm i vtpl

Template Syntax

if / else

{{if expr1}}
	expr1 is true
{{else if expr2}}
	expr1 is false and expr2 is true
{{else}}
	expr1 and expr2 are both false
{{/if}}

each

{{each arrayOrObject as v, k}}
	{{ v }}
{{/each}}

interpolation

{{ v }}
{{ v + 'tail' }}
{{ a || b }}
{{ a && b }}
{{ Date.now() }}
{{ Math.round( num ) }}
{{ boolean ? a : b }}
...

All interpolations will be escaped automatically by default, if you want to disable this feature, add = at the start of your interpolation

For example

{{= expr}}

You can also disable this feature in global scope, by adding following code before compiling your template

v.config( 'escape', false );

filter

{{ expr | filter1: param1, param2 | filter2 | ... }}

If you want to register your own filter, use v.registerFilter

Here is an example for filter1 above

v.registerFilter( 'filter1', function( str, param1, param2 ) {
	return str + param1 + param2;
} );

API

v.compile( template, options )

template

Type: string

options

options here has priority over global config

key value
openTag string
closeTag string
escape boolean

v.registerFilter( name, fn )

name

Type: string

fn

Type: function

v.config( key, value )

key

Type: string

value

Type: string or boolean

key value
openTag string
closeTag string
escape boolean

License

MIT © fengzilong

Package Sidebar

Install

npm i vtpl

Weekly Downloads

9

Version

1.1.2

License

MIT

Unpacked Size

44.1 kB

Total Files

7

Last publish

Collaborators

  • fengzilong