vte

velocity-template-engine

🚀Velocity template engine for JavaScript and PHP.

Showing:

Popularity

Downloads/wk

83

GitHub Stars

28

Maintenance

Last Commit

1yr ago

Contributors

3

Package

Dependencies

0

Size (min+gzip)

8.7KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Velocity Template Engine, as a DSL Interpreter across Multi- Languages

中文文档

>> JavaScript Version Only <<

Implementations of Apache Velocity template engine,including versions of JavaScript and PHP.

Quick Start

JavaScript Version

Import file velocity.js, then use module velocity which contains methods render and compile, both of which can be used as pure functions.

rendering a template

Method render combines a velocity template string (vts below for short) and a data object, returning a string.

Example:

var tmpl = 'My name is ${name}. I\'m a #if($gender == "male")boy#{else}girl#end.';
var data = {
    name: 'June',
    gender: 'female'
};
window.velocity.render(tmpl, data); // "My name is June. I'm a girl."

compiling a template to a function

Method compile compiles a vts to a pure function or a string of pure function body (to be written into files).

Example:

var tmpl = 'My name is ${name}. I\'m a #if($gender == "male")boy#{else}girl#end.';
var render = window.velocity.compile(tmpl);

// The second argument is options, and the `raw` property indicates whether to compile the vts to a string or not.
var render_raw = window.velocity.compile(tmpl, { raw: true });

var data = {
    name: 'June',
    gender: 'female'
};

render(data); // "My name is June. I'm a girl."
(new Function(render_raw))(data); // "My name is June. I'm a girl."

PHP Version

Import file velocity.php, then use Main class in the namespace PhpVelocity. The Main class contains the constructor and method render.

The constructor is used to specify the compilation path and set the update checking switch of template files (with suffix .vm).

Method render combines the vts (in the path-specified file) and a data object, returning a string.

<?php
include './velocity.php';
use PhpVelocity\Main as Velocity;

$compile_dir = 'path/to/compiles';

// Specify the compilation path, and set the update checking switch for templates. 
// If a template file is modified after its latest compilation, it should be re-compiled before being used for rendering.
$ve = new Velocity($compile_dir, true); 

$data = array("name" => "June", "gender" => "female");
echo $ve->render('path/to/template1.vm', $data); // Render a template and a data object.
echo $ve->render('path/to/template2.vm'); // Render a template.
$data = array("name" => "Apple", "price" => 10000);
echo $ve->render('path/to/template3.vm', $data, true); // Render a template and a data object, and clean up the history data.

Develop & Test

ScriptFunction
/server.jsRun the server of the web application for debugging and testing.
/run_tests.shRun the test runners in different language versions (JavaScript and Java for now), and run the output diffing.
/diff_output.shCompare (namely diff) each case's output of test runners in different languages (JavaScript and Java for now), and generate a report file.
/sync_dists.shBuild and distribute.
/build_*.jsBuild. Compile *.pegjs grammar to a parser, then combine it with base codes, and generate the complete velocity library file `/src/[javascript

Project Structure

PathExplanation
/src/antlrantlr parser generator development for velocity template grammar.
/src/debugThe WYSIWYG web application for debugging and testing.
/src/*Source codes of velocity implementation in different programming languages, and all dependencies.
/build/*/velocity.*Distribution codes, built from src files.
/test/casesTest cases, each of which has a .vm file and a .json file.
/test/diffDiffing the output results of test runners in different language versions, generating file result.html.
/test/*/srcSource codes of test runners in different language versions. Test runners run upon test cases as the input, and output into output folders of each language version.
/test/*/run.shThe test launcher for each language version.
/test/*/output/*.htmlOutputs of test runners in different language versions.

More

License

MIT

Copyright © 2017-present, shenfe

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100