Mustache.js is a logicless template library that let's you flow JSON data onto an existing page. You can have as many templates as you want on a page, so you can take the same JSON data and display it in different ways. Using the library couldn't be easier. You create a template and then bring the JSON file and flow it into an area of your page using the template as a guide. Want to know more about mustache? Make sure you check out this website or download the library directly from github. I'm going to go my document, and onto my server I have loaded a couple of additional files.
You can see that I have an images folder with some images that I'm going to use later on, coming from the template as well as a data.json template. I've got this one open right here. You can see that it is a speakers object, and the speakers object has an array of element. You could see it right here. It's always a good idea to make sure that when you create a JSON document it's formatted perfectly, or you won't be able to do anything with it. Let me go ahead and copy this, and I'm going to switch over to the JSON Editor Online. And what I want to do is paste that right here, then hit this button.
That button will move it over from this side into this side. Here I can make sure that I take a look at the structure of my objects. I know this is one object called speakers. It has nine elements and if I expand that, I can see each element and expand any of the elements to see what the structure would be. Now, I know that my JSON file is fine because it would've shown me an error if it wasn't formatted properly, so that's great! Let's go ahead and go into our index file and first create the template that we're going to use to display this data.
First off, I'll start by creating a placeholder for the speakers, and I'll put a pound sign right here. So this is the first part of the template. I need to close it, kind of like how I close an HTML element. Now, where am I getting this from? This actually comes from my data file, and it's the name of my object. So what mustache is going to do is it's going to cycle through all the speakers in my object and apply the template that I'm going to type in here. I'll create a div with a class of speaker, and I'm going to close that just like I would any other div. Then I'm going to add an image src.
It is going to be my images folder, and then the name that I call here, shortname, as well as _tn.jpg. Where am I getting this from? That is the name of one of these elements called shortname. Shortname is pretty much the name up here without any special characters, so it has an underscore instead of a space. That's how my images are named. If you open up my images, you'll see that Barot is named with his short name, plus _tn.jpg, and that's what I'm doing here.
I'll add an Alt tag, and in here I'll pull in the name of this user. You can probably tell why the library is called mustache.js. It uses these little curly brackets that look like mustaches on their side, and it uses these as placeholders to target different JSON elements in your files. Now, we'll type in an h3. We'll also need an h4, so I'll do that one as well. And that will have the name and this other one will have the reknown.
Finally, we'll do a paragraph and that will have the bio. These are all names of fields in our data.json document. And the template is really nothing more than just HTML with the placeholders for the template. Now, we need to create another script. This one has to go after the libraries load, because this is going to use these libraries and this is going to be a normal script, so it's not going to refer to an external file.
Here we'll start off by doing that function that executes whenever jQuery loads. In here, we'll use jQuery's getJSON function. That's going to load our data file, and when that data file is received, then it's going to execute a function literal. It's going to read the contents of data.json into a data object.
We'll create a variable called template. That variable is going to load the contents of the template that we created up here. I'm going to use jQuery to target the speakerstpl, which is what I call the template up there, and read the HTML into this template variable. Here's the part where I'm actually going to use mustache.js. I'm going to create an HTML variable, and into that variable I'm going to call the mustache.to_html function for mustache.js, and feed that the template that I read into the template variable, as well as the data that we received from the JSON file.
Mustache to HTML will process the data and feed it into the template and then create some HTML. Once we have that HTML, then we can feed it into a div. We created the div with an id of carousel to feed that data into, and so we'll modify its HTML to be the HTML that we received from mustache.js. So, this id right here is referring to this div id right here. It's going to feed all that data into that. If we did everything correctly, I'm going to save this, and I'm going to come into my page and I'll refresh it, and we should see all the data from our JSON file fall in here.
jQuery Data with AJAX170,322 Views
PHP with MySQL Essential Training4,283,533 Views
Git Essential Training2,861,219 Views
jQuery Essential Training1,042,234 Views
Up and Running with AngularJS 1845,758 Views