sigur7
sigur7

Reputation: 936

Mustache.js - JSON items not rendering

This is my first look at mustache.js in anticipation of using it for a larger project.

The problem I have is the spreadsheet JSON output I plan to use on my larger project doesn't have a name for the array. So based on the tutorial I followed on Lynda.com, ideally my array data would be called "fruits" but as you can see from my JSON data below, it outputs with no name for the array, just [].

https://api.myjson.com/bins/czrxt

https://docs.google.com/spreadsheets/d/1KIg84G9CXErw2bWhkEHWUkOI4CR-biFeLqCtdypaLU8/edit?usp=sharing

How can I render my set of data in mustache.js if there is no array name that can be used to output the template?

<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 &copy; 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>

Upvotes: 2

Views: 1045

Answers (1)

Keith Hafer
Keith Hafer

Reputation: 26

I see three issues in your code:

  1. The href in your template should be href="{{{wiki}}}". Note that the 3 curly brackets prevent the forward slashes and other special characters in the URL from being encoded (e.g. "/" -> "%2f").
  2. The src parameter in the template should be src="{{{image}}}"
  3. As mentioned previously, pass {fruits:data} as the second parameter to the Mustache.to_html function.

The revised code is:

<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 &copy; 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>

Upvotes: 1

Related Questions