-
-
Notifications
You must be signed in to change notification settings - Fork 357
Introduce a modern, JSX-like syntax for TwigComponents #2662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,7 +15,7 @@ | |||||||||||||||||||||
use Twig\Lexer; | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Rewrites <twig:component> syntaxes to {% component %} syntaxes. | ||||||||||||||||||||||
* Rewrites <twig:component> or <Component> syntaxes to {% component %} syntaxes. | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
class TwigPreLexer | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
|
@@ -28,17 +28,34 @@ class TwigPreLexer | |||||||||||||||||||||
*/ | ||||||||||||||||||||||
private array $currentComponents = []; | ||||||||||||||||||||||
|
||||||||||||||||||||||
public function __construct(int $startingLine = 1) | ||||||||||||||||||||||
public function __construct(int $startingLine = 1, private readonly bool $withShortTags = false) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
$this->line = $startingLine; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public function preLexComponents(string $input): string | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
if (!str_contains($input, '<twig:')) { | ||||||||||||||||||||||
// tag may be: | ||||||||||||||||||||||
// - prefixed: <twig:componentName> | ||||||||||||||||||||||
// - short (jsx like): <ComponentName> (with a capital letter) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
$isPrefixedTags = str_contains($input, '<twig:'); | ||||||||||||||||||||||
$isShortTags = $this->withShortTags && preg_match_all('/<([A-Z][a-zA-Z0-9_:-]+)([^>]*)>/', $input, $matches, \PREG_SET_ORDER); | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (!$isPrefixedTags && !$isShortTags) { | ||||||||||||||||||||||
return $input; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
if ($isShortTags) { | ||||||||||||||||||||||
$componentNames = array_map(fn ($match) => $match[1], $matches); | ||||||||||||||||||||||
$componentNames = array_unique(array_filter($componentNames)); | ||||||||||||||||||||||
Comment on lines
+50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead, can we use a single |
||||||||||||||||||||||
|
||||||||||||||||||||||
// To simplify things in the rest of the class, we replace the component name with twig:<componentName> | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
foreach ($componentNames as $componentName) { | ||||||||||||||||||||||
Comment on lines
+50
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe better, 1 loop instead of 4?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be even easier in the other way.... not sure we need the 2step PATH There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me, changing the code might actually make it slower. Your solution helps reduce iterations if all the nodes are unique. But if a page contains the same node multiple times, it will trigger multiple I think it's better to first reduce the array to a unique list, then perform the minimal number of That said, happy to discuss it of course! |
||||||||||||||||||||||
$input = preg_replace('!<(/?)'.preg_quote($componentName).'!', '<$1twig:'.lcfirst($componentName), $input); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
$this->input = $input = str_replace(["\r\n", "\r"], "\n", $input); | ||||||||||||||||||||||
$this->length = \strlen($input); | ||||||||||||||||||||||
$output = ''; | ||||||||||||||||||||||
|
@@ -394,7 +411,7 @@ private function consumeBlock(string $componentName): string | |||||||||||||||||||||
} | ||||||||||||||||||||||
$blockContents = $this->consumeUntilEndBlock(); | ||||||||||||||||||||||
|
||||||||||||||||||||||
$subLexer = new self($this->line); | ||||||||||||||||||||||
$subLexer = new self($this->line, $this->withShortTags); | ||||||||||||||||||||||
$output .= $subLexer->preLexComponents($blockContents); | ||||||||||||||||||||||
|
||||||||||||||||||||||
$this->consume($closingTag); | ||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.