Skip to content

Fix {add $var}content{/add} generates invalid PHP - #353

Merged
maxisoft-git merged 1 commit into
masterfrom
fix/add-tag-output
Apr 23, 2026
Merged

Fix {add $var}content{/add} generates invalid PHP#353
maxisoft-git merged 1 commit into
masterfrom
fix/add-tag-output

Conversation

@maxisoft-git

Copy link
Copy Markdown
Member

Fix for Issue #348

Problem

When using {add \$var}content{/add} without explicit value assignment, the generated PHP code was invalid:

$var = if(!isset($var)) {
    ob_get_clean();
}

This caused a parse error: "syntax error, unexpected token 'if'".

Solution

Changed setClose in Compiler.php to wrap the ENTIRE assignment in the IF condition, not just the value part:

if(!isset($var)) {
    $var = ob_get_clean();
} else {
    ob_end_clean();
}

This correctly:

  • Captures content into variable if variable is NOT set
  • Discards buffer without outputting if variable IS already set

Tests Added

  • {add $v}Hello{/add} Result: {$v} - basic capture
  • {add $v}Value: {$x}{/add} - capture with variable interpolation
  • {set $v = "first"}{add $v}second{/add} - verify {add} does NOT overwrite existing variable

Fixes

Fixes #348

When using {add $var}content{/add} without explicit value assignment,
the generated code was invalid: '$var = if(!isset($var)) { ... }'

Fixed by wrapping the entire assignment in setClose, not just the value:
- If variable is NOT set: capture content into variable
- If variable IS set: discard buffer with ob_end_clean()

Added 3 tests to cover:
- {add $v}Hello{/add} basic capture
- {add $v}Value: {$x}{/add} capture with interpolation
- {set $v = "first"}{add $v}second{/add} verify no overwrite

Fixes #348
@maxisoft-git maxisoft-git mentioned this pull request Apr 23, 2026
@maxisoft-git
maxisoft-git merged commit af88554 into master Apr 23, 2026
1 check passed
@maxisoft-git
maxisoft-git deleted the fix/add-tag-output branch April 23, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ошибка {add}

1 participant