Skip to content

Compiler::section creates code which throws "Undefined variable $sp" #7

@robincafolla

Description

@robincafolla

Version: "devtheorem/php-handlebars": "^0.9.8"
PHP Version: PHP 8.4.12

The Compiler::section() method generates functions like this for an {{#each}} block:

function($cx, $in) {return ' * '.LR::encq($in).'
'.$sp.'';}

When {{#each}} is called in a partial that function is then wrapped in another function

'phpDoc' => function ($cx, $in, $sp) {return ''.$sp.''.((LR::ifvar($in['phpDoc'] ?? null, false)) ? '/**
'.$sp.''.((LR::ifvar($in['phpDoc']['description'] ?? null, false)) ? ''.LR::sec($cx, $in['phpDoc']['description'] ?? null, null, $in, true, function($cx, $in) {return ' * '.LR::encq($in).'
'.$sp.'';}).'' : '').' */
'.$sp.'' : '').'';}

The wrapping function is passed in $sp, which is the whitespace to put before each line. However the each function isn't passed the whitespace, and the generated function doesn't have a use ($sp) to allow it to access the variable from outside its scope.

This means that the template throws warnings (and presumably that whitespace is lost inside the each blocks)

PHP Warning: Undefined variable $sp in /path/to/vendor/devtheorem/php-handlebars/src/Handlebars.php(41) : eval()'d code on line 29

partial used for testing:

{{#if phpDoc}}
/**
    {{#if phpDoc.description}}
        {{#each phpDoc.description}}
 * {{this}}
        {{/each}}
    {{/if}}
 */
{{/if}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions