zordius / lightncandy

An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ),
https://zordius.github.io/HandlebarsCookbook/
MIT License
608 stars 76 forks source link

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

Open DanielSiepmann opened 1 year ago

DanielSiepmann commented 1 year ago

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