Skip to content

each cannot cross a simple array [key => value] ? #318

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

Open
ghost opened this issue Sep 1, 2019 · 2 comments
Open

each cannot cross a simple array [key => value] ? #318

ghost opened this issue Sep 1, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented Sep 1, 2019

The PHP Code:

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

// The Template:
$template = <<<VAREND
{{#each table as |value key|}}
KEY: {{dump key}}
VAL: {{dump value}}
{{/each}}
VAREND;

// Helpers:
$helpers = array(
  'dump' => function ($x) {
    return var_export($x, true);
  },
);

// Partials:
$partials = array(
    'bar' => 'Yes, partial',
);

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

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

// Input Data:
$data = array(
  'table' => array(
    'foo' => 'bar'
  )
);

// 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 Issue:

An array like ['key' => 'value'] cannot be cross by an each...
It's work if you value is an array, but not a string...

Can you solve this ?

@Krinkle
Copy link
Contributor

Krinkle commented Oct 14, 2019

The Handlebars "each" syntax is best for iterating key-value objects in an array. That way, every key comes from your data and has a name that you can find easy:

$table = [
  [ 'a' => 'foo', 'b' => 'bar' ],
  [ 'a' => 'baz', 'b' => 'quux' ],
];
{{#each table}}
A: {{a}}
B: {{b}}
{{/each}}
$table = [
  [ 'key' => 'foo', 'value' => 'bar' ],
  [ 'key' => 'baz', 'value' => 'quux' ],
];
{{#each table}}
KEY: {{key}}
VALUE: {{value}}
{{/each}}

It is also possible to access the whole current value, with the {{.}} syntax.
And the current key index, via the {{@index}} syntax.

See also http://handlebarsjs.com/builtin_helpers.html#iteration
and https://zordius.github.io/HandlebarsCookbook/0019-each.html.

For example, your data can be displayed like this:

$data = array(
  'table' => array(
    'foo' => 'bar'
  )
);
{{#each table}}
KEY: {{@index}}
VAL: {{.}}
{{/each}}

@theodorejb
Copy link

The key => value array works as expected in PHP Handlebars (a modern fork focused on Handlebars spec compliance):

use DevTheorem\Handlebars\{Handlebars, Options};

require 'vendor/autoload.php';

$template = <<<VAREND
{{#each table as |value key|}}
KEY: {{dump key}}
VAL: {{dump value}}
{{/each}}
VAREND;

$templateFn = Handlebars::compile($template, new Options(
    helpers: [
        'dump' => fn(mixed $x) => var_export($x, true),
    ],
));

echo $templateFn([
    'table' => ['foo' => 'bar'],
]);

Output:

KEY: 'foo'
VAL: 'bar'

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

2 participants