项目作者: shenfe

项目描述 :
🚀Velocity template engine for JavaScript and PHP.
高级语言: PHP
项目地址: git://github.com/shenfe/Velocity.git
创建时间: 2017-03-29T12:02:49Z
项目社区:https://github.com/shenfe/Velocity

开源协议:

下载


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:

  1. var tmpl = 'My name is ${name}. I\'m a #if($gender == "male")boy#{else}girl#end.';
  2. var data = {
  3. name: 'June',
  4. gender: 'female'
  5. };
  6. 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:

  1. var tmpl = 'My name is ${name}. I\'m a #if($gender == "male")boy#{else}girl#end.';
  2. var render = window.velocity.compile(tmpl);
  3. // The second argument is options, and the `raw` property indicates whether to compile the vts to a string or not.
  4. var render_raw = window.velocity.compile(tmpl, { raw: true });
  5. var data = {
  6. name: 'June',
  7. gender: 'female'
  8. };
  9. render(data); // "My name is June. I'm a girl."
  10. (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.

  1. <?php
  2. include './velocity.php';
  3. use PhpVelocity\Main as Velocity;
  4. $compile_dir = 'path/to/compiles';
  5. // Specify the compilation path, and set the update checking switch for templates.
  6. // If a template file is modified after its latest compilation, it should be re-compiled before being used for rendering.
  7. $ve = new Velocity($compile_dir, true);
  8. $data = array("name" => "June", "gender" => "female");
  9. echo $ve->render('path/to/template1.vm', $data); // Render a template and a data object.
  10. echo $ve->render('path/to/template2.vm'); // Render a template.
  11. $data = array("name" => "Apple", "price" => 10000);
  12. echo $ve->render('path/to/template3.vm', $data, true); // Render a template and a data object, and clean up the history data.

Develop & Test

Script | Function
| :—-: | :—- |
/server.js | Run the server of the web application for debugging and testing.
/run_tests.sh | Run the test runners in different language versions (JavaScript and Java for now), and run the output diffing.
/diff_output.sh | Compare (namely diff) each case’s output of test runners in different languages (JavaScript and Java for now), and generate a report file.
/sync_dists.sh | Build and distribute.
/build_*.js | Build. Compile *.pegjs grammar to a parser, then combine it with base codes, and generate the complete velocity library file /src/[javascript|php]/velocity.[js|php].

Project Structure

Path | Explanation
| :—-: | :—- |
/src/antlr | antlr parser generator development for velocity template grammar.
/src/debug | The 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/cases | Test cases, each of which has a .vm file and a .json file.
/test/diff | Diffing the output results of test runners in different language versions, generating file result.html.
/test/*/src | Source 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.sh | The test launcher for each language version.
/test/*/output/*.html | Outputs of test runners in different language versions.

More

License

MIT

Copyright © 2017-present, shenfe