Skip to content

Instantly share code, notes, and snippets.

@RaphaelBlehoue
Created September 14, 2014 09:47
Show Gist options
  • Save RaphaelBlehoue/3aa912196eb832d7ecb3 to your computer and use it in GitHub Desktop.
Save RaphaelBlehoue/3aa912196eb832d7ecb3 to your computer and use it in GitHub Desktop.
Bonjour j'utilise Mustache et j'aimerai afficher mes données recuperées en ajax avec le template mustache , mais je ne compren pas pourquoi celui ne s'affiche pas : Voila mon code - j'utilise cakephp
//données recuperées en JSON
[
{
"User": {
"lastname": "wild",
"firstname": "bertrand",
"email": "wild@outlook.fr",
"entreprise": "alpha",
"fonction": "ScrumMaster"
}
},
{
"User": {
"lastname": "micheal",
"firstname": "jhon",
"email": "johnd@outlook.fr",
"entreprise": "beta",
"fonction": "developer"
}
}
]
// Chargement du fichier JS et code pour afficher les données avec template Mustache
<?php echo $this->Html->script('mustache',array('inline'=>false)); ?>
$(window).load(function(){
var url = '<?php echo $this->Html->url(array('controller' => 'Users','action' => 'getuser')); ?>';
var tpl;
$.get(url,{},function(data){
$('#loading').hide();
var tpl = $("#tpluser").html();
var htmls = Mustache.render(tpl, data);
$("#member").append(htmls);
},"json");
});
//Template Mustache
<div class="hidden" id="tpluser">
{{#User}}
<h1>{{lastname}} {{firstname}}</h1>
<span>{{fonction}}</span>
<span>{{email}}</span>
{{/User}}
</div>
@germain-gg
Copy link

{
  "Users" : [
  {
      "lastname": "wild",
      "firstname": "bertrand",
      "email": "wild@outlook.fr",
      "entreprise": "alpha",
      "fonction": "ScrumMaster"
  },
  {
      "lastname": "micheal",
      "firstname": "jhon",
      "email": "johnd@outlook.fr",
      "entreprise": "beta",
      "fonction": "developer"
  }
]
}

Avec cette structure de donnée ça devrait mieux marcher je pense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment