0

I'm using mustache.php. Suppose I have an HTML structure like the following (just an example to let you understand my problem) in my mustache template:

<nav>
    <div>
        <div>
            <a>{{object1.title}}</a>
        </div>
        <menu>
            {{#object2.items}}
                <li><a>{{title}}</a></li>
            {{/object2.items}}
        </menu>
    </div>
</nav>

My problem here is that my data that the template should use comes from two different sources (object1 and object2).

According to mustache.php wiki I could load a template like so:

<?php

$object1 = some_function_returns_a_json_object( 1 );

$mustache = new Mustache_Engine();
$my_template = $mustache->loadTemplate( 'my_template' ); // will fetch my_template.mustache
echo $my_template->render( $object1 ); // passes data from $object1 to my_template.mustache

But how about my menu items in the html example above? they need to be sourced from another object which I can't have in the same $object1.

I haven't understood how to deal with one template (and partials) and data from multiple sources.

Should I split the HTML above in different partials? Even so, how do I pass the json data to nested partials anyway?

unfulvio
  • 826
  • 3
  • 19
  • 34

1 Answers1

0

oh just figured it out

I can pass my objects as an array here

echo $my_template->render( array( 'object1' => $object1, 'object2' => $object2 ) );

and it will work - wasn't too clear in mustache.php docs

not sure if it's the best practice but looks good also for use in partials

unfulvio
  • 826
  • 3
  • 19
  • 34