Skip to content

Instantly share code, notes, and snippets.

@aoloe
Created November 13, 2015 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoloe/eb2c5b01930410dc848b to your computer and use it in GitHub Desktop.
Save aoloe/eb2c5b01930410dc848b to your computer and use it in GitHub Desktop.
mustache with partials and associative arrays
{
"name": "aoloe/mustache-test",
"require" : {
"mustache/mustache" : "~2.5"
}
}
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require('vendor/autoload.php');
$associative = true;
if ($associative) {
$list = ['list' => [
1 => [
'name' => 'hanna',
'sub' => [
2 => [
'name' => 'pico',
'sub' => []
],
]
],
3 => [
'name' => 'her',
'sub' => []
],
4 => [
'name' => 'sisters',
'sub' => []
],
]
];
} else {
$list = ['list' => [
[
'name' => 'hanna',
'sub' => [
[
'name' => 'pico',
'sub' => []
],
]
],
[
'name' => 'her',
'sub' => []
],
[
'name' => 'sisters',
'sub' => []
],
]
];
}
$mustache = new \Mustache_Engine(
array (
'loader' => new \Mustache_Loader_FilesystemLoader('./'),
)
);
$template = $mustache->loadTemplate('list-ul');
echo $template->render(
array (
'list' => new \ArrayIterator($list['list']),
'no-entries' => 'nothing to see',
)
);
<li>{{name}}
{{#sub}}
<ul>
{{>list-li}}
</ul>
{{/sub}}
</li>
<ul>
{{#list}}
{{>list-li}}
{{/list}}
{{^list}}
{{no-entries}}
{{/list}}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment