twig-to-phtml
raw JSON → 0.3.4 verified Fri May 01 auth: no javascript
A Twig to PHTML transpiler that converts Twig template syntax into PHP (PHTML) code. Version 0.3.4 is the current stable release, with infrequent updates. It differs from alternatives like twig2php by focusing on direct transpilation without runtime dependencies. Designed for build-time use in PHP projects migrating from Twig to native PHP templates. Supports basic Twig constructs like variable output, loops, and conditionals.
Common errors
error TypeError: Transpiler is not a constructor ↓
cause Imported as default instead of named export.
fix
Use import { Transpiler } from 'twig-to-phtml'
error Cannot find module 'twig-to-phtml' ↓
cause Package not installed or not in node_modules.
fix
Run npm install --save-dev twig-to-phtml
error Unexpected token '{{' in PHTML output ↓
cause Twig syntax not transpiled; likely transpile() was not called.
fix
Call transpiler.transpile(twigString) to convert.
Warnings
breaking The transpile() method may not support all Twig constructs; test thoroughly. ↓
fix Check the documentation for supported tags and filters. Use only basic syntax.
deprecated Some Twig features may be deprecated in future releases; keep package updated. ↓
fix Monitor the GitHub repository for breaking changes and new releases.
gotcha The transpiler does not validate PHTML syntax; output may require manual fixes. ↓
fix Always review generated PHTML code before using in production.
Install
npm install twig-to-phtml yarn add twig-to-phtml pnpm add twig-to-phtml Imports
- Transpiler wrong
import Transpiler from 'twig-to-phtml'correctimport { Transpiler } from 'twig-to-phtml' - Transpiler wrong
const Transpiler = require('twig-to-phtml')correctconst { Transpiler } = require('twig-to-phtml') - Transpiler wrong
import { type Transpiler } from 'twig-to-phtml'correctimport type { Transpiler } from 'twig-to-phtml'
Quickstart
import { Transpiler } from 'twig-to-phtml';
const transpiler = new Transpiler();
const phtml = transpiler.transpile('{{foo}}');
console.log(phtml); // '<?=$foo?>'