- Javascript: Intermediate level
- HTML: Intermediate level
"My site is living in 2001. I have a pantry full of ajax powered spaghetti, and basically everything is a new page refresh. How can I made it slick?"
Because there are problems with Thick Servers.
{{ content }}
<div> Name: {{ customerName }} </div>
<script id="header" type="text/x-handlebars-template">
<div> Name: {{ headerTitle }} </div>
</script>
var theData = {
customers:[
{firstName:”John”, lastName:”Doe”, age:20}, { {firstName:”Chuck”, lastName:”Norris”, age:25}
]};
<script id="header" type="text/x-handlebars-template">
{{#each customers}}
<li> {{ firstName }} {{ lastName }} </li>
{{/each}}
</script>
var theTemplate = Handlebars.compile (templateScript);
var html = theTemplate (theData);
$(document.body).append (html);
<html>
<head>
<script type="text/javascript" src="jquery-1.9.1.min.js">
</script>
</head>
<body>
The List of Shoes:
<ul class="shoesNav"></ul>
</body>
</html>
$(function () {
var shoesData = [{name:"Nike", price:199.00 }, {name:"Loafers", price:59.00 }, {name:"Wing Tip", price:259.00 }];
function updateAllShoes(shoes) {
var theHTMLListOfShoes = "";
shoesData.forEach (function (eachShoe) {
theHTMLListOfShoes += '<li class="shoes">' +
'<a href="/' + eachShoe.name.toLowerCase() +
'">' + eachShoe.name + ' -- Price: ' + eachShoe.price
'</a></li>';
});
return theHTMLListOfShoes;
}
$(".shoesNav").append (updateAllShoes(shoesData));
});
$(function () {
var shoesData = [{name:"Nike", price:199.00 },
{name:"Loafers", price:59.00 },
{name:"Wing Tip", price:259.00 }];
var theTemplateScript = $("#shoe-template").html();
var theTemplate = Handlebars.compile (theTemplateScript);
$(".shoesNav").append (theTemplate(shoesData));
});
<script id="shoe-template" type="x-handlebars-template">
{{#each this}}
<li class="shoes">
<a href="/{{name}}">{{name}} -- Price: {{price}} </a>
</li>
{{/each}}
</script>
{{#each}}
Content goes here.
{{/each}}
{{#if someValueIsTrue}}
Content goes here
{{/if}}
var objData = {
name: {
firstName: "Michael",
lastName:"Jackson"
}
}
{{name.firstName}}
{{> partialName}}
var data = {
people: [
"Chuck Norris",
"Bruce Lee",
"Jackie Chan"
]}
<script id="peopleTemplate" type="text/x-handlebars-template">
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
</script>
var data = {
firstName: "Chuck",
lastName: "Norris"
}
<script id="peopleTemplate" type="text/x-handlebars-template">
<ul class="people_list">
{{#each this}}
<li>{{firstName}} {{lastName}}</li>
{{/each}}
</ul>
</script>
var data = {
user: ["Bond"]
}
<script id="peopleTemplate" type="text/x-handlebars-template">
<ul class="people_list">
{{#if user.length}}
<li>{{user}}</li>
{{/if}}
</ul>
</script>
var data = {
firstName: "Chuck",
lastName: "Norris"
}
<script id="peopleTemplate" type="text/x-handlebars-template">
<ul class="people_list">
{{#fullName this}}
</ul>
</script>
<div> Chuck Norris </div>
Handlebars.registerHelper('fullName', function(person){
return person.first + ' ' + person.last
});
Refactor the multiple-choice quizz code given on the last talks using Handlebars. Keep in mind the following:
var shoesData = {
groupName:"Celebrities",
users: [{name:{first:"John", last:"Doe" }},
{name:{first:"John", last:"Waters" }} ]};
<script id="shoe-template" type="x-handlebars-template">
{{#users}}
<li>
{{name.first}} {{name.last}} is in the {{../groupName}} group
</li>
{{/users}}
</script>
The rendered HTML will be:
Mike Alexander is in the Celebrities group.
John Waters is in the Celebrities group.