Skip to content

Instantly share code, notes, and snippets.

@trafficinc
Created May 20, 2018 23:42
Show Gist options
  • Save trafficinc/21b5cfa53be1305e474faed6da572242 to your computer and use it in GitHub Desktop.
Save trafficinc/21b5cfa53be1305e474faed6da572242 to your computer and use it in GitHub Desktop.
Simple JS Templating w/o framework
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Template</title>
</head>
<body>
<div class="current-price"></div>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<!-- js template -->
<script id="current-price-template" type="application/html-template">
<div class="current-price">
<span>Current Price: {{CurrentPrice}}</span><br />
<span>[{{BidCount}} bids]</span>
</div>
</script>
<script>
var template = $('#current-price-template').clone().html();
var data = {
CurrentPrice: 23.00,
BidCount: 7
};
var html = template
.replace('{{CurrentPrice}}', data.CurrentPrice)
.replace('{{BidCount}}', data.BidCount);
$('.current-price').replaceWith(html);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment