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

Iterator support esoteric behavior (keys) #9

Closed
ktomk opened this issue Aug 3, 2010 · 4 comments
Closed

Iterator support esoteric behavior (keys) #9

ktomk opened this issue Aug 3, 2010 · 4 comments

Comments

@ktomk
Copy link

ktomk commented Aug 3, 2010

I just run over this yesterday while using Mustache. I think it's worth to share if someone runs into the same problem. I had an iterator that was keyed starting with 1.

While using it for a Mustache section, the section was rendered (once) but did not contain any sub values (Not using a pragma).

I wondered about that and checked out the tests in specific the one which iterates over an array. I rebuild that test scenario in my template and used the testdata as a comparison which did work. The testdata array is default keyed.

As this was the only difference I changed my keying to start with 0 instead of 1. This did the trick for the moment.

While I wanted to reproduce this yesterdays behavior to make a more specific issue report here today, I was not able to reproduce this any longer (!).

So if someone runs over a similar problem, in the end the fix was easy for me. I'm a bit disappointed because I can not reproduce it right now even when I revoke the changes I made to fix the problem.

@Yahasana
Copy link

In the implicit_iterator/ImplicitIterator.php, make a little change to the start key of the array. it'll not work.
class ImplicitIterator extends Mustache {
protected $data = array(1 => 'Donkey Kong', 'Luigi', 'Mario', 'Peach', 'Yoshi');
}

@bobthecow
Copy link
Owner

The current functionality is by design: Because PHP uses the same primitive for hashes and arrays, Mustache needs some way of determining whether a variable is iterable (i.e. an indexed array) or not (i.e. an associative array).

The current implementation assumes that if an array does not have consecutive, zero-based array keys, it must be an associative array. And, if an array is an associative array, it is not "iterable" in the mustache sense of the word.

@Yahasana
Copy link

Why not let the associative array iterated ? I cannt see the syntax {{%IMPLICIT-ITERATOR}}{{.}} in the mustache manual. however, you have implemented in mustache.php, you do good job. why not let that much more powerful? I just want to know that it's a technique problem or is going against the muastache philosophy.

@bobthecow
Copy link
Owner

Actually, it's not a matter of letting this implementation of Mustache be 'more powerful'. The current functionality is fundamental to Mustache. Given this view:

$view = array(
    'things' => array(
        array('name' => 'foo'),
        array('name' => 'bar'),
        array('name' => 'baz'),
    ),
    'stuff' => array(
        'name' => 'qux',
    ),
);

And this template:

{{things}}
 * {{name}}
{{/things}}

You'll get something like:

 * foo
 * bar
 * baz

But if you use the stuff variable instead:

{{stuff}}
 * {{name}}
{{/stuff}}

You would get:

 * qux

This is because, in Mustache, a section is either for or if, depending on the value of the variable referenced. The templates above resolve to (approximately) this in PHP:

<?php foreach ($things as $thing): ?>
 * <?php echo $thing['name']; ?>
<?php endforeach; ?>

and

<?php if (!empty($view['stuff'])): ?>
 * <?php echo $view['stuff']['name']; ?>
<?php endif; ?>

In the case of {{stuff}}, if we iterated over associative arrays it would render this instead:

 *

You could see how this is a problem, right?

This issue was closed.
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

3 participants