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

Rendering records from idiorm #22

Closed
tutuca opened this issue Nov 15, 2010 · 5 comments
Closed

Rendering records from idiorm #22

tutuca opened this issue Nov 15, 2010 · 5 comments

Comments

@tutuca
Copy link

tutuca commented Nov 15, 2010

Hi, I've been playing with mustache in combination with idiorm and slim.

So I have this view:

function test(){
    $albums = ORM::for_table('album')->find_many();

    Slim::render(
        'test.html',
        array(
            'albums' => $albums,
        )
    );
}

where albums are records from the database that has two columns, "name" and "description".

Naively, I've tried this in test.html:

{{#albums}}
   {{album.name}}
{{/albums}}

or a combination of that kind of things. I don't know much of idiorm or mustache's internals and I'm new to PHP myself, but for what I get to see my problem relies in that I don't know the value of the inner object in the loop (the index inside the #albums section) so I can't access it's variables or anything.
Mustache doesn't provide any way to map the inner object to a variable, as would result with a foreach loop (which in turn works as expected):

foreach ($albums as $album ){
    echo $album->name;
}

How would you solve this situation?

PD: I'm not really sure if this is not the place to ask this questions :)
PD: I have the complete app here

@j4mie
Copy link

j4mie commented Nov 15, 2010

I can't test this right now, but I think that Mustache promotes the scope of the object inside the loop, so the correct syntax would be:

{{#albums}}
    {{name}}
{{/albums}}

Sorry if you've tried this already..

@tutuca
Copy link
Author

tutuca commented Nov 15, 2010

Yes, I've tried that too. and would seem logical to do that. I thought that was a problem with the structure of the ORM object, so I did a test using the complex example provided with mustache, something like this (roughly, will get the proper example later):

public $person = array(
    'name' => array('first' => 'Chris', 'last' => 'Firescythe'),
    'age' => 24,
    'hometown' => array(
        'city'  => 'Cincinnati',
        'state' => 'OH',
    )
);

    public $persons = array($person, $person, $person)

and in mutache
{{#persons}}
{{name.first}}
{{age}}
{{/persons}}

but only age is rendered. Turning all the parser's exceptions to true tells that name is an unknown variable

@j4mie
Copy link

j4mie commented Nov 15, 2010

I've found the problem - on line 609 of Mustache.php, it's using isset to check whether a property exists on the object. I hadn't implemented the __isset magic method in Idiorm, so this wasn't working. I've fixed this in Idiorm (commit).

The following code example now works:

$sql = 'CREATE TABLE person (first_name text, surname text)';

ORM::get_db()->exec($sql);

ORM::for_table('person')->create(array('first_name' => 'fred', 'surname' => 'bloggs'))->save();
ORM::for_table('person')->create(array('first_name' => 'joe', 'surname' => 'jackson'))->save();

$data = array('people' => ORM::for_table('person')->find_many());

$template = '
    {{#people}}
        {{first_name}} {{surname}}
    {{/people}}
';

$m = new Mustache();

echo $m->render($template, $data);

@tutuca
Copy link
Author

tutuca commented Nov 15, 2010

I can confirm that the fix is working. More an idiorm issue than mustache's sorry for reporting here and thanks j4mie :)

@bobthecow
Copy link
Owner

No worries, glad you got it figured out.

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