1 | # Twing
|
2 | [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage percentage][coveralls-image]][coveralls-url] [![Donate][donate-image]][donate-url]
|
3 |
|
4 | First-class Twig engine for Node.js
|
5 |
|
6 | ## Philosophy behind Twing
|
7 |
|
8 | We believe that a first-class Twig engine should be able to render any template to the exact same result as the official PHP engine. That means that it should implement 100% of the syntax defined by the language specifications and that it should render that syntax using PHP logic.
|
9 |
|
10 | We also believe that a first-class Twig engine should be able to catch-up easily when Twig specifications evolve. Its code architecture and philosophy should then be as close as possible as the PHP implementation.
|
11 |
|
12 | Finally, we believe that a first-class Twig engine should allow users to build on their experience with TwigPHP and get support from the huge community that comes with it.
|
13 |
|
14 | That's what Twing is. A maintainability-first engine that pass 100% of the TwigPHP integration tests, is as close as possible to its code structure and expose an as-close-as-possible API.
|
15 |
|
16 | ## Prerequisites
|
17 |
|
18 | Twing needs at least **node.js 6.0.0** to run but it is highly recommended to use **node.js 8.x** or higher to enjoy the best performance possible.
|
19 |
|
20 | ## Installation
|
21 |
|
22 | The recommended way to install Twing is via npm:
|
23 |
|
24 | `npm install twing --save`
|
25 |
|
26 | ## Basic API Usage
|
27 |
|
28 | ```js
|
29 | const {TwingEnvironment, TwingLoaderArray} = require('twing');
|
30 |
|
31 | let loader = new TwingLoaderArray({
|
32 | 'index.twig': 'Hello {{ name }}!'
|
33 | });
|
34 | let twing = new TwingEnvironment(loader);
|
35 |
|
36 | let output = twing.render('index.twig', {name: 'Fabien'});
|
37 | ```
|
38 |
|
39 | ## Usage with Express
|
40 |
|
41 | Twing and Express work quite well together. Have a look at the [documentation](http://NightlyCommit.github.io/twing/intro.html#real-world-example-using-express) for an example of usage with Express.
|
42 |
|
43 | ## Browser support
|
44 |
|
45 | Starting with version 2.0.0, Twing can be used in web browsers with very few compromise. Filesystem components are obviously not available (namely filesystem loader and cache) but everything else is fully supported.
|
46 |
|
47 | ### Module bundler
|
48 |
|
49 | Module bundlers will automatically grab the browser-specific flavor of Twing when Twing module is imported. Either `const {TwingEnvironment} = require('twing');` or `import {TwingEnvironment} from 'twing';` will work in both node.js and the browser - once bundled in the latter case.
|
50 |
|
51 | ### Script tag
|
52 |
|
53 | Use [jsdelivr](https://www.jsdelivr.com/) CDN to include Twing in your HTML document:
|
54 |
|
55 | `<script src="https://cdn.jsdelivr.net/npm/twing/dist/lib.min.js"></script>`
|
56 |
|
57 | Once loaded by the browser, Twing is available under the global `Twing` variable.
|
58 |
|
59 | ## Twig specifications implementation
|
60 |
|
61 | Twing aims at implementing Twig specifications perfectly, without compromise. This is not an easy task due to the nature of Twig specifications: they don't exist officially and can only be deduced from the public documentation, the source code documentation and the test suite of the PHP reference implementation. It sometimes happens that something that was not part of either the documentations or the test suite suddenly becomes part of the specifications like the [`filter` tag](https://github.com/twigphp/Twig/issues/3091) or the [macros rework](https://github.com/twigphp/Twig/issues/3090) issues, putting Twing and all other non-reference implementations in the uncomfortable position of having to deal with a potential breaking change. Since Twig's team doesn't plan on releasing some official specifications for the language, we can't expect the problem to be solved anytime soon.
|
62 |
|
63 | Twing's strategy here is to stick strictly to Semantic Versioning rules and *never* introduce a breaking change into a minor version - its extensive test suite with 100% code coverage guarantees that. Twig teams's mistakes will be managed by either issuing a [known issue](#known-issues), if the mistake is trivial, or bumping to a new major version, if it is not.
|
64 |
|
65 | ### Compatibility chart
|
66 |
|
67 | Here is the compatibility chart between minor versions of Twing and Twig specifications levels, along with a summary of notable features provided by each Twig specifications level. Note that Twig minor versions don't always provide new language-related features (because of Twig's team perpetuating the confusion between Twig and their reference implementation, TwigPHP).
|
68 |
|
69 | |Twing version|Twig specifications level|Notable features|
|
70 | |:---:|:---:|---|
|
71 | |3.0|2.11|[Macros scoping](https://twig.symfony.com/doc/2.x/tags/macro.html#macros-scoping)|
|
72 | |2.3|2.10|`spaceless`, `column`, `filter`, `map` and `reduce` filters, `apply` tag, `line whitespace trimming` whitespace control modifier|
|
73 | |2.2|2.6|`deprecated` tag|
|
74 | |1.3|2.5|`spaceless` and `block`-related deprecations|
|
75 | |1.0|2.4| |
|
76 |
|
77 | It is highly recommended to always use the latest version of Twing available as bug fixes will always target the latest version.
|
78 |
|
79 | ### Known issues
|
80 |
|
81 | You can find the list of known issues of Twing regarding Twig specifications implementation [here](http://NightlyCommit.github.io/twing/known_issues). Note that known issues are guaranteed to be addressed in the next major version bump of Twing.
|
82 |
|
83 | ## More information
|
84 |
|
85 | Read the [documentation](http://NightlyCommit.github.io/twing) for more information.
|
86 |
|
87 | ## Related projects
|
88 |
|
89 | * [gulp-twing](https://www.npmjs.com/package/gulp-twing): Compile Twig templates with gulp. Build upon Twing.
|
90 | * [twing-loader](https://www.npmjs.com/package/twing-loader): Webpack loader that compiles Twig templates using Twing.
|
91 |
|
92 | ## Contributing
|
93 |
|
94 | * Fork this repository
|
95 | * Code
|
96 | * Implement tests using [tape](https://github.com/substack/tape)
|
97 | * Issue a pull request keeping in mind that all pull requests must reference an issue in the issue queue
|
98 |
|
99 | ## License
|
100 |
|
101 | Copyright © 2018 [Eric MORAND](https://github.com/ericmorand). Released under the [2-Clause BSD License](https://github.com/ericmorand/twing/blob/master/LICENSE).
|
102 |
|
103 | [npm-image]: https://badge.fury.io/js/twing.svg
|
104 | [npm-url]: https://npmjs.org/package/twing
|
105 | [travis-image]: https://travis-ci.com/NightlyCommit/twing.svg?branch=master
|
106 | [travis-url]: https://travis-ci.com/NightlyCommit/twing
|
107 | [coveralls-image]: https://coveralls.io/repos/github/NightlyCommit/twing/badge.svg
|
108 | [coveralls-url]: https://coveralls.io/github/NightlyCommit/twing
|
109 | [donate-image]: https://img.shields.io/badge/Donate-PayPal-green.svg
|
110 | [donate-url]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7YZU3L2JL2KJA
|