这是我第一次看到mustache.js,希望将它用于更大的项目。
我遇到的问题是我计划在我的大型项目中使用的电子表格JSON输出没有数组的名称。因此,基于我在Lynda.com上关注的教程,理想情况下我的数组数据将被称为“水果”,但正如您可以从下面的JSON数据中看到的那样,它输出的数据没有名称,只有[]。
https://api.myjson.com/bins/czrxt
https://docs.google.com/spreadsheets/d/1KIg84G9CXErw2bWhkEHWUkOI4CR-biFeLqCtdypaLU8/edit?usp=sharing
如果没有可用于输出模板的数组名称,如何在mustache.js中呈现我的数据集?
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Mustache Template</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/thumbnail-gallery.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href=""></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<!--
<li class="nav-item active">
<a class="nav-link" href="#">Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
-->
</ul>
</div>
</div>
</nav>
<!-- Page Content -->
<div class="container">
<h1 class="my-4 text-center text-lg-left">Fruits/h1>
<div id="fruitfeed" class="row text-center text-lg-left"></div>
<script id="fruittpl" type="text/template">
{{#fruits}}
<div class="col-lg-3 col-md-4 col-xs-6">
<a href="{{image}}" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="{{wiki}}" alt="">
</a>
</div>
{{/fruits}}
</script>
</div>
<!-- /.container -->
<!-- Footer -->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright © 2017</p>
</div>
<!-- /.container -->
</footer>
<!-- Bootstrap core Javascript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/popper/popper.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!-- Mustache.js -->
<!--<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>-->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.cycle/2.9999.8/jquery.cycle.all.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.0/mustache.min.js"></script>
<script type="text/javascript">
$(function() {
$.getJSON('https://api.myjson.com/bins/czrxt', function(data) {
var template = $('#fruittpl').html();
var html = Mustache.to_html(template, {fruits:data});
$('#fruitfeed').html(html);
}); //getJSON
}); //function
</script>
</body>
</html>
答案 0 :(得分:1)
我在您的代码中看到了三个问题:
修订后的代码是:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Mustache Template</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/thumbnail-gallery.css" rel="stylesheet">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href=""></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<!--
<li class="nav-item active">
<a class="nav-link" href="#">Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
-->
</ul>
</div>
</div>
</nav>
<!-- Page Content -->
<div class="container">
<h1 class="my-4 text-center text-lg-left">Fruits/h1>
<div id="fruitfeed" class="row text-center text-lg-left"></div>
<script id="fruittpl" type="text/template">
{{#fruits}}
<div class="col-lg-3 col-md-4 col-xs-6">
<a href="{{wiki}}" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="{{image}}" alt="">
</a>
</div>
{{/fruits}}
</script>
</div>
<!-- /.container -->
<!-- Footer -->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright © 2017</p>
</div>
<!-- /.container -->
</footer>
<!-- Bootstrap core Javascript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/popper/popper.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!-- Mustache.js -->
<!--<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>-->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.cycle/2.9999.8/jquery.cycle.all.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.0/mustache.min.js"></script>
<script type="text/javascript">
$(function() {
$.getJSON('https://api.myjson.com/bins/czrxt', function(data) {
var template = $('#fruittpl').html();
var html = Mustache.to_html(template, {fruits:data});
$('#fruitfeed').html(html);
}); //getJSON
}); //function
</script>