Skip to content
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

PHP Warning: Undefined array key when trying to access unavailable key from parent context #363

Open
DanielSiepmann opened this issue Sep 20, 2022 · 0 comments

Comments

@DanielSiepmann
Copy link

The PHP Code:

require('./vendor/autoload.php');
use LightnCandy\LightnCandy;

error_reporting(E_ALL);

// The Template:
$template = <<<VAREND
{{#each itemList as |item|}}
    <svg class="m-stage__copyright-icon svg"><use xlink:href="{{../global.svgSprite}}#icon-camera"></use></svg>
{{/each}}
VAREND;

$phpStr = LightnCandy::compile($template, array(
  // Used compile flags
  'flags' => LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_HANDLEBARS,
  'helpers' => [],
  'partials' => [],
));

echo "Generated PHP Code:\n$phpStr\n";

// Input Data:
$data = array(
    'itemList' => [
        0 => 'item1',
    ],
    // 'global' => [
    //     'svgSprite' => 'somePath'
    // ],
);

// Save the compiled PHP code into a php file
file_put_contents('render.php', '<?php ' . $phpStr . '?>');

// Get the render function from the php file
$renderer = include('render.php');

echo "Result:\n" . $renderer($data);

The result

Generated PHP Code:
use \LightnCandy\SafeString as SafeString;use \LightnCandy\Runtime as LR;return function ($in = null, $options = null) {
    $helpers = array();
    $partials = array();
    $cx = array(
        'flags' => array(
            'jstrue' => false,
            'jsobj' => false,
            'jslen' => false,
            'spvar' => true,
            'prop' => false,
            'method' => false,
            'lambda' => false,
            'mustlok' => false,
            'mustlam' => false,
            'mustsec' => false,
            'echo' => false,
            'partnc' => false,
            'knohlp' => false,
            'debug' => isset($options['debug']) ? $options['debug'] : 1,
        ),
        'constants' => array(),
        'helpers' => isset($options['helpers']) ? array_merge($helpers, $options['helpers']) : $helpers,
        'partials' => isset($options['partials']) ? array_merge($partials, $options['partials']) : $partials,
        'scopes' => array(),
        'sp_vars' => isset($options['data']) ? array_merge(array('root' => $in), $options['data']) : array('root' => $in),
        'blparam' => array(),
        'partialid' => 0,
        'runtime' => '\LightnCandy\Runtime',
    );

    $inary=is_array($in);
    return ''.LR::sec($cx, (($inary && isset($in['itemList'])) ? $in['itemList'] : null), array('item'), $in, true, function($cx, $in) {$inary=is_array($in);return '    <svg class="m-stage__copyright-icon svg"><use xlink:href="'.LR::encq($cx, ((isset($cx['scopes'][count($cx['scopes'])-1]) && is_array($cx['scopes'][count($cx['scopes'])-1]['global']) && isset($cx['scopes'][count($cx['scopes'])-1]['global']['svgSprite'])) ? $cx['scopes'][count($cx['scopes'])-1]['global']['svgSprite'] : null)).'#icon-camera"></use></svg>
';}).'';
};
PHP Warning:  Undefined array key "global" in /home/daniels/Projects/stuff/lightncandy/render.php on line 32
Result:
    <svg class="m-stage__copyright-icon svg"><use xlink:href="#icon-camera"></use></svg>

The Issue:

A PHP Warning on PHP 8.1 due to checking is_array() prior checking if the value exists at all.
https://zordius.github.io/HandlebarsCookbook/0014-path.html

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

No branches or pull requests

1 participant