Skip to content
This repository has been archived by the owner on Dec 2, 2018. It is now read-only.
/ jsVelocity Public archive
forked from vapour/jsVelocity

一个JavaScript实现的velocity模板引擎

Notifications You must be signed in to change notification settings

esmerio/jsVelocity

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 

Repository files navigation

#jsVelocity - A JavaScript Template Engine of Velocity

Where to Use?

You can use velocity.js rendering stuff in various scenarios. E.g. you can render templates in your browser.

Usage

A quick example how to use velocity.js:

Template.define('t1', [
    '#set(${name}="vapour")',
    '#if(${name})',
    '<div>${name}</div>',
    '#end',
    '<ul>',
    '#foreach(${web} in ${website})',
    '<li>${web.name} : ${web.url}</li>',
    '#end',
    '</ul>'
]);

var html = Template.render('t1', {
    website: [{
      name: 'google',
      url: 'http:https://www.google.com/'
    }, {
      name: 'yahoo',
      url: 'http:https://www.yahoo.com/'
    }, {
      name: 'facebook',
      url: 'http:https://www.facebook.com/'
    }, {
      name: 'twitter',
      url: 'http:https://www.twitter.com/'
    }]
});

variables

variables are always surrounded by curly brace like this ${name}.

Template.define('t1', [
    '<div>${name}</div>',
    '<div>${web}</div>'
]);
var html = Template.render('t1', {
    name: 'vapour',
    web: 'http:https://www.dovapour.info/' 
});

#set

to assign a value to a variable

Template.define('t1', [
    '#set(${name}="vapour")',
    '<div>${name}</div>',
    '<div>${web}</div>'
]);
var html = Template.render('t1', {
   web: 'http:https://www.dovapour.info/' 
});

#if #else #end

Conditional expression begin with #if and end with #end. When condition evaluates to true, the true section is rendered, otherwise the false section will output.

Template.define('t1', [
    '#set(${name}="vapour")',
    '#if(${name})',
    '<div>${name}</div>',
    '#else',
    '<div>${web}</div>',
    '#end'
]);
var html = Template.render('t1', {
   web: 'http:https://www.dovapour.info/' 
});

#foreach && #macro

Template.define('t1', [
    '<ul>',
    '#foreach(${web} in ${website})',
    '<li>${velocityCount} ${web.name} : ${web.url} #macro(isGoogle ${web.name})</li>',
    '#end',
    '</ul>'
]);
    
var html = Template.render('t1', {
    website: [{
        name: 'google',
        url: 'http:https://www.google.com/'
    }, {
        name: 'yahoo',
        url: 'http:https://www.yahoo.com/'
    }, {
        name: 'facebook',
        url: 'http:https://www.facebook.com/'
    }, {
        name: 'twitter',
        url: 'http:https://www.twitter.com/'
    }],
    isGoogle: function(name){
        return name === 'google' ? ', is google' : '';
    }
});

About

一个JavaScript实现的velocity模板引擎

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published