Created
November 13, 2015 14:47
-
-
Save aoloe/eb2c5b01930410dc848b to your computer and use it in GitHub Desktop.
mustache with partials and associative arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "aoloe/mustache-test", | |
"require" : { | |
"mustache/mustache" : "~2.5" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<li>{{name}} | |
{{#sub}} | |
<ul> | |
{{>list-li}} | |
</ul> | |
{{/sub}} | |
</li> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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