Skip to content

Problem to access array item with numeric keys. specially key === 0 #16

@vladimirfalconpiva

Description

@vladimirfalconpiva

Hi,

Thanks for the new release!

I'd like to report a regression introduced in the latest version: accessing an array item by key 0 (zero) no longer works. This was supported in version 0.9.x.

Reproduction case:

<?php
require_once '/var/www/html/app/vendor/autoload.php';

use DevTheorem\Handlebars\Handlebars;
use DevTheorem\Handlebars\Options;

$template = <<<HBS
Page title if key === 0: {{this.0.title}}
Page title if key === 1: {{this.1.title}}
Page title if key === 2: {{this.2.title}}

HBS;

$opts = new Options();
$compiled = Handlebars::precompile($template, $opts);
$renderer = Handlebars::template($compiled);

// Simulate the context that RenderHelper passes: the children array directly
$children = [
    0 =>['title' => 'Page A', 'link' => '/a', 'lvl' => 1, 'active' => false, 'current' => false, 'children' => []],
    1 =>['title' => 'Page B', 'link' => '/b', 'lvl' => 1, 'active' => true, 'current' => true, 'children' => []],
    2 =>['title' => 'Page C', 'link' => '/c', 'lvl' => 1, 'active' => false, 'current' => false, 'children' => []],
];

$result = $renderer($children, []);

echo "=== RENDER OUTPUT ===\n";
echo $result;
echo "\n=== END OUTPUT ===\n";

Actual output:

=== RENDER OUTPUT ===
Page title if key === 0: 
Page title if key === 1: Page B
Page title if key === 2: Page C

=== END OUTPUT ===

Expected output: {{this.0.title}} should resolve to Page A, consistent with the behavior of keys 1 and 2.

It appears that a zero-based key is being treated as falsy and skipped during lookup. Keys 1 and 2 resolve correctly.

Thanks for looking into this — and for all the work on the library!

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