RFC: Native Markup Expressions in PHP
Liam Hammett proposes a JSX-like syntax for HTML fragments as first-class PHP expressions.
Markup expressions compile to plain `new` calls at compile time, support component composition with capitalized tag names, and escape output by default. Discussion focuses on component resolution heuristics and parser extensibility.
Liam Hammett has opened discussion on an RFC proposing Native Markup Expressions, a feature that adds JSX-inspired syntax for HTML fragments directly into PHP.
The proposal introduces a compact syntax for defining and composing HTML markup as language expressions. Lowercase tag names represent literal HTML elements, while capitalized tag names, namespace separators, or static method references denote components. All embedded expressions are escaped by default for security.
A `<button class="btn">Sign in</button>` expression compiles to `new \Markup\Element('button', ['class' => 'btn'], ['Sign in'])` at compile time. This means markup meaning is determined during compilation, not at runtime. Components implementing the `Markup\Html` interface can be invoked like `<Greeting name="Rasmus" />` and automatically render their return value.
A working implementation with tests is available in pull request php/php-src#22661.
Key design decisions under discussion include the capitalization heuristic for distinguishing components from HTML elements. Garrett W. questioned whether this approach—borrowed from JavaScript's JSX, where lowercase means HTML and capitalized means component—is appropriate for PHP, given that PSR-4 is not a binding standard and lowercase class names are legal. Liam responded that the heuristic prevents silent bugs from typos, allows compile-time resolution without autoloader overhead, and provides a performance benefit for the common case of mostly HTML with sprinkled components.
Other discussants, including Edmond Dantes, suggested PHP could benefit from generalized parser hooks enabling extensions to define custom DSL syntaxes rather than locking in a single markup syntax. Edmond noted that such extensibility could enable SQL-like LINQ expressions and attribute-based metaprogramming patterns. Liam acknowledged these are separate considerations from the current RFC scope.
Readers’ Forum
No contributions yet — open the debate.
Open the discussion
No account or password needed — just enter your e-mail and we’ll send you a one-time sign-in link. First time here? You’re set up automatically.
Your rating will be applied automatically after you sign in.
Check your inbox
We’ve sent a sign-in link to …. Open it on this device — this tab will sign you in automatically.
Waiting for your click …
·