$ show twigjs/twig.js
Twig.js is a JavaScript port of the Twig templating language, originally written in PHP. It provides a full-featured template engine for both Node.js and browser environments, implementing core Twig syntax: variable interpolation ({{ }}), logic tags ({% %}), comments ({# #}), template inheritance via extends/block, includes, embeds, macros, and a rich set of filters, functions, and tests. The library ships as a UMD bundle for browsers and CommonJS for Node, and integrates with Express as a view engine via __express and renderFile. A CLI (bin/twigjs) compiles .twig templates into standalone JavaScript modules for Node, AMD, or CJS2 targets. The architecture is modular: src/twig.core.js handles tokenization, compilation, and template caching; src/twig.expression.js implements an expression tokenizer and shunting-yard parser for operators, filters, and function calls; src/twig.logic.js handles control-flow tags; and src/twig.filters.js / src/twig.functions.js provide the standard filter/function libraries. A factory pattern (src/twig.factory.js) assembles all submodules, and an extension API (extendTag, extendFilter, extendFunction, extendTest) allows users to add custom behavior. The codebase is well-tested with an extensive suite covering control structures, inheritance, expressions, filters, loaders, parsers, and browser-specific behavior. The repository carries legacy baggage: mixed ES5/ES6 style, a custom synchronous Promise implementation (Twig.Promise) that is not fully spec-compliant, and incomplete parity with Twig 3.x features (several filters/functions marked unsupported in implementation notes). The project migrated from Travis CI to GitHub Actions (Node 22/24) and maintains active development, but the custom async layer and dependency on locutus (PHP function ports) are areas of potential fragility. Demos include an Express note-taking app and a Backbone.js Twitter feed, both using older patterns but useful as integration references.
The LLM identified 4 files worth highlighting for reuse:
Implements a complete expression tokenizer and shunting-yard algorithm for parsing Twig expressions into postfix notation, then evaluating them with operator precedence, filters, function calls, and array/object literals. The shunting-yard pattern is a classic, self-contained algorithm that could be extracted for any expression-evaluation DSL, and the module demonstrates non-obvious parsing skill.
Provides a custom synchronous Promise/thenable implementation (Twig.Promise, Twig.Thenable, Twig.FullPromise) that allows template rendering to be fully synchronous by default while optionally mixing with native async operations. This is a novel pattern for libraries that need to preserve synchronous API guarantees while supporting async extensions, and is isolated enough to extract.
Central module implementing the tokenize → compile → prepare rendering pipeline, plus template caching (Twig.Templates.registry), loader/parser registration, and the Twig.Template class. The tokenization and compilation pipeline is a clean, extensible pattern for building template engines or DSL compilers, with well-defined extension hooks.
Programmatic API for compiling .twig template files into standalone JavaScript modules (Node, AMD, CJS2 formats). Demonstrates a clean separation between parsing and code generation, and is reusable as a reference for any template-to-JS compilation pipeline or build tooling.