XaminProject / handlebars.php

Handlebars processor for php
331 stars 134 forks source link

Iterator block and list block throw exceptions #1

Closed joshpangell closed 11 years ago

joshpangell commented 11 years ago

I first tried using the standard {{#}} iterator method:

$h = new Handlebars_Engine;
echo $h->render(
        'Planets:<br> {{#planets}}<h6>{{planet}}</h6>{{/planets}}', 
        array(
            'planets' => array(
                'planet' => "Mercury",
                'planet' => "Venus",
                'planet' => "Earth",
                'planet' => "Mars"
            )
        )
    );

Error:

Fatal error: Uncaught exception 'RuntimeException' with message 'planet is not registered as a helper' in Handlebars/Template.php:172

When that did not work out, I tried to use the {{#list}} iterator:

$h = new Handlebars_Engine;
echo $h->render(
        'Planets:<br> {{#list planets}}<h6>{{planet}}</h6>{{/list}}', 
        array(
            'planets' => array(
                'planet' => "Mercury",
                'planet' => "Venus",
                'planet' => "Earth",
                'planet' => "Mars"
            )
        )
    );

Error:

Fatal error: Uncaught exception 'RuntimeException' with message 'list is not registered as a helper' in Handlebars/Template.php:172

everplays commented 11 years ago

actually we don't have the mustache compatibility implemented yet so it won't iterate over planets if you do {{#planets}} ... {{/planets}}. also as far as I know list is not within built-in helpers you should use each instead or add list yourself. for built-in helpers take a look at http://handlebarsjs.com/#builtins

btw this works:

$h = new Handlebars_Engine;
echo $h->render(
    'Planets:<br> {{#each planets}}<h6>{{planet}}</h6>{{/each}}', 
    array(
        'planets' => array(
            array('planet' => "Mercury"),
            array('planet' => "Venus"),
            array('planet' => "Earth"),
            array('planet' => "Mars")
        )
    )   
);

you can do the same with:

$h = new Handlebars_Engine;
echo $h->render(
    'Planets:<br> {{#each planets}}<h6>{{.}}</h6>{{/each}}',
    array(
        'planets' => array("Mercury", "Venus", "Earth","Mars")
    )
);
fzerorubigd commented 11 years ago

Except for {{^variable}} any other mustache like markup are available.

echo $h->render(
    'Planets:<br> {{#planets}}<h6>{{.}}</h6>{{/planets}}',   //like {{#each planets}}
    array(
        'planets' => array("Mercury", "Venus", "Earth","Mars")
    )
);

with :

echo $h->render(
    'Planets:<br> {{#planets}}<h6>{{.}}</h6>{{/planets}}',   //like {{#with planets}}
    array(
        'planets' => new Object()
    )
);

if

echo $h->render(
    'Planets:<br> {{#planets}}<h6>{{.}}</h6>{{/planets}}',   //like {{#if planets}}
    array(
        'planets' => false
    )
);
fzerorubigd commented 11 years ago

fixed in e3c29366941172842473434bc97bcddb3bbe8307