The Velocity template engine is available in JavaScript and PHP. See velocity.apache.org for the official Java version of Apache.
Quick to use
After introducing the velocity.js file (which supports modularization), the global variable velocity (exported if it is a module) contains two properties: the render pure function and the compile pure function.
Template rendering
The Render method combines a VM template string and a data object into a string, for 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."Copy the code
The compile method compiles a VM template string into a function (or function body string, for writing to a JS file), for example:
var tmpl = 'My name is ${name}. I\ 'm a #if($gender == "male")boy#{else}girl#end.';
var render = window.velocity.compile(tmpl);
var render_raw = window.velocity.compile(tmpl, { raw: true }); //The second argument is the configuration item and returns a string if raw is 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."Copy the code
PHP version
After the velvelocity. PHP file is introduced, the Main class of the namespace PhpVelocity sets the compilation path and the vm file update check switch through the constructor. The RENDER method synthesizes a string of vm files and data in the specified path, and can be set to clear past data. Such as:
include './velocity.php';
use PhpVelocity\Main as Velocity;
$compile_dir = 'path/to/compiles';
$ve = new Velocity($compile_dir.true); //Set the compile path and check the VM template file for updates before each render (no updates are checked by default) and recompile if there are any
$data = array("name" = > "June"."gender" = > "female");
echo $ve->render('path/to/template1.vm'.$data); //Render templates and data
echo $ve->render('path/to/template2.vm'); //Apply colours to a drawing template
$data = array("name" = > "Apple"."price" = > 10000);
echo $ve->render('path/to/template3.vm'.$data.true); //Render templates and data, clear past dataCopy the code
The script | role |
---|---|
/server.js |
Web programs that run WYSIWYG. |
/run_tests.sh |
Test program, responsible for running each language version runner (currently js and Java two versions of the comparison), monitor runner output file changes, rundiff_output . |
/diff_output.sh |
Compare the different runner outputs of each case (currently the comparison between JS and Java versions) and output reports. |
/sync_dists.sh |
willsrc Package and copy each language version of Velocity tobuild neutralizationtest In the. |
/build_*.js |
will/src/[javascript/php] (corresponding language version source directory)*.pegjs Syntax file build into parser, combined with source code, generated/src/[javascript/php]/velocity.[js/php] . |
The project structure
The path | meaning |
---|---|
/src/antlr |
Antlr Paradigm development for Velocity. |
/src/debug |
Web programs for development and debugging. |
/src/* |
Implementation source code for Each language version of Velocity, including all dependencies. |
/build/*/velocity.* |
Each language version of the Velocity executable should be provided bysrc Packaging. |
/test/cases |
Test cases, each containing one of the same name.vm Files and a.json File. |
/test/diff |
The result comparator compares the output of each language version of TestRunner, and is generated in this directoryresult.html Report file. |
/test/*/src |
TestRunner source code for each language version; The TestRunner runtime does not require input parameters and simply reads case files and prints them to their respective filesoutput Directory. |
/test/*/run.sh |
Test run scripts for each language version. |
/test/*/output/*.html |
Output of TestRunner for each language version, one case for each output file. |
- velocity
- pegjs
- phpegjs
License
Copyright (c) 2017-present, shenfe