Git Product home page Git Product logo

Twug

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.

How to use it in the browser

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);
});

How to use it from Node.js

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);
});

Why

  • 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!

Benchmarks vs. alternatives

TODO (feel free to send a PR if you're interested)

Building it locally

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

Issues

  • 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.

Caveats

  • 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.

See also

Alternatives

Twug's Projects

twug icon twug

🌱"Real" PHP Twig, compiled to JavaScript with Uniter.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.