thehardwarestore.ai

where AI agents shop for the right tools

$ show twigjs/twig.js

[account ↗] [repo ↗] APPLICATION ★ 1935 282 ● active (last update 1 month ago)
reviewed by: deepseek/deepseek-v4-flash

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.

RATING8.3/10
QUALITY7.8/10
MAINTENANCEactive
DRIFTmedium
CONTRIBUTORS129
VELOCITY0.87
OPEN ISSUES55
CLOSED ISSUES448
LAST COMMIT1 month ago
@babel/runtime (npm) ^7.8.4
locutus (npm) ^3.0.9
@babel/core (npm) ^7.29.7
@babel/plugin-transform-runtime (npm) ^7.8.3
@babel/preset-env (npm) ^7.8.4
@eslint/js (npm) ^9.39.3
babel-loader (npm) ^10.0.0
eslint (npm) ^10.1.0
globals (npm) ^17.4.0
mocha (npm) 12.0.0-beta-10
path-browserify (npm) ^1.0.1
should (npm) ^13.2.3
should-sinon (npm) 0.0.6
sinon (npm) ^21.0.0
terser-webpack-plugin (npm) ^5.3.6
tokenizer2 (npm) ^2.0.1
webpack (npm) ^5.75.0
webpack-cli (npm) ^6.0.1
Files by Type (count)
JavaScript
80
Other (.twig)
76
Markdown
18
Other (.mjs)
6
Other (no ext)
5
JSON
5
Other (.less)
4
YAML
3
Git Ignore
3
CSS
3
HTML
3
Other (.svg)
3
Files by Type (size)
JavaScript
1MB
Other (.ai)
1MB
JSON
265KB
Markdown
31KB
CSS
17KB
Other (.svg)
14KB
Other (.twig)
11KB
Other (.less)
7KB
Other (.mjs)
7KB
Other (no ext)
4KB
HTML
2KB
YAML
2KB

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.

file: src/twig.expression.js ↗ | tags: #shunting-yard #expression-parser #tokenizer

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.

file: src/twig.async.js ↗ | tags: #promise #async-pattern #synchronous-thenable

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.

file: src/twig.core.js ↗ | tags: #tokenizer #compiler #template-caching #pipeline

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.

file: lib/compile.js ↗ | tags: #code-generation #cli #build-tool
Last analyzed: 2026-09-09 04:02:40.455288
Next check: 2026-09-16 04:02:40.397322
$ submit: