Twig PHP (https://twig.symfony.com/) compiled to JavaScript with Uniter.
Note 1: This is an experimental release. Expect things to be broken (see Issues below), performance to be sub-optimal and the compiled JS bundle size to be rather large for now.
Note 2: This is primarily a demonstration of how Uniter is able to handle complex projects like Twig. If you want to use Uniter to build the rest of your client-side logic, you can skip using Twug.js and just require Twig.php with Composer as part of your build.
Install it with NPM:
npm install twug
Require it and render some Twig:
let twug = require('twug').default;
twug.renderString('Hello there {{ name }}!', {name: 'Fred'}).then((renderedHtml) => {
document.body.insertAdjacentHTML('beforeEnd', renderedHtml);
}, (error) => {
document.body.insertAdjacentHTML('beforeEnd', 'Twug error: ' + error);
});
Install it with NPM:
npm install twug
Require it and render some Twig:
let twug = require('twug').default;
twug.renderString('Hello there {{ name }}!', {name: 'Fred'}).then((renderedText) => {
console.log(renderedText);
}, (error) => {
console.log('Twug error: ' + error);
});
- Get all the features of "real" PHP Twig
- Demonstrate how a PHP project built with Uniter can be "invisibly" published to the NPM ecosystem
- Have fun!
TODO (feel free to send a PR if you're interested)
Clone the repository
git clone https://github.com/twug/twug
Install PHP dependencies with Composer
composer install
Install JS dependencies with NPM
npm install
Start a development build (note this will be in watch mode)
build:dev:watch
- An old Twig release (1.x) is being used
- The deprecated string loader is being used
- The escape filter is currently broken due to missing
preg_replace_callback(...)
support in Uniter - A whole bunch of notices are currently printed due to some missing/shimmed PHP functions and constants.
- Some features of PHP are not yet implemented by Uniter: there are stubs for these
in
php/src/shims.php
. These will all need to be removed and implemented properly in Uniter before a stable version of this package could be tagged.
- Uniter (https://github.com/asmblah/uniter)
- PHPify (part of Uniter) (https://github.com/uniter/phpify)
- Twig.js (https://github.com/twigjs/twig.js)
- Twing (https://github.com/ericmorand/twing)