n:attribute Support in php-to-latte.php - #4
Open
NoNoNo wants to merge 1 commit into
Open
Conversation
**Supported structures:**
- `{if}` → `n:if`
- `{elseif}` → `n:elseif`
- `{else}` → `n:else`
- `{foreach}` → `n:foreach`
- `{for}` → `n:for`
- `{while}` → `n:while`
- `{try}` → `n:try`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR significantly enhances the
php-to-latte.phpconverter with intelligent n:attribute notation support, conditional attribute handling, and nested if optimization. The converter now produces cleaner, more idiomatic Latte templates.Changes Overview
1. n:attribute Notation for Control Structures (LattePrinter.php)
Control structures that wrap single HTML elements are now converted to n:attribute notation:
Before:
After:
Supported structures:
{if}→n:if{elseif}→n:elseif{else}→n:else{foreach}→n:foreach{for}→n:for{while}→n:while{try}→n:try2. Conditional Attributes to n:attr (PhpConverter.php)
PHP templates with conditional output inside HTML attributes are now converted to
n:attrnotation:Input:
Output:
Features:
isset()and!empty()conditions$obj->property), array access ($arr['key']), and simple variablesn:attr(cleaner output)3. Nested If Merging (PhpConverter.php)
Nested if statements are automatically merged into a single condition:
Input:
Output:
4. Enhanced String to HTML Conversion (PhpConverter.php)
The
stringToHtml()transformation now handles:echo '<div>' . $var . '</div>')echo $user->id)5. Self-Closing Element Support (LattePrinter.php)
Void/self-closing HTML elements (
<input>,<br>,<img>, etc.) are now properly detected for n:attribute conversion even without closing tags.Files Changed
src/PhpConverter.php(+694 lines)convertConditionalAttributes()- transforms conditional attributes to n:attrmergeNestedIfs()- merges nested if statementsstringToHtml()- better echo to HTML conversionexpandConcat()- skips concatenations with string literalssrc/LattePrinter.php(+598 lines)canGenerateNAttribute*()helperstests/fixtures-php/n-attributes- 9 new testsTest Results
all pass
Backward Compatibility
The changes are backward compatible - existing conversions that don't match the new patterns continue to work as before. The new features are additive optimizations.
Examples
Example 1: Simple conditional input
↓
Example 2: Control structure wrapping element
↓
Example 3: Nested ifs
↓
Example 4: Loop with single element
↓
Example 5: Try-catch with single element
↓
Technical Details
AST Transformation Pipeline
The conversion pipeline in
PhpConverter::convert()now runs:expandEcho()- Split multiple echoesremoveHtmlSpecialChars()- Remove redundant htmlspecialcharsexpandConcat()- Expand non-HTML concatenationsremoveHtmlSpecialChars()- Second passmergeNestedIfs()- Merge nested if statements (NEW)convertConditionalAttributes()- Transform conditional attributes (NEW)stringToHtml()- Convert string echoes to HTMLPattern Detection
The converter uses AST analysis to detect patterns:
attr="+ If with isset/!empty + InlineHTML starting with"Future Improvements
Potential future enhancements:
n:elsewith n:attr attributes{first}/{last}patternsn:ifset(Latte 3.x specific)