Created
May 20, 2018 23:42
-
-
Save trafficinc/21b5cfa53be1305e474faed6da572242 to your computer and use it in GitHub Desktop.
Simple JS Templating w/o framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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