Created
February 1, 2016 07:32
-
-
Save selvagsz/2635614c87412f77d829 to your computer and use it in GitHub Desktop.
Theming with Handlebars
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
nav { | |
margin-top: 100px; | |
padding: 10px; | |
background: #ddd; | |
color: #666 | |
} | |
.form-group { | |
margin: 20px; | |
} | |
label { | |
display: block; | |
margin-bottom: 5px; | |
} |
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> | |
<script src="https://code.jquery.com/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Theming with handlebars</title> | |
<style id="custom-theming"> | |
#themed-nav { | |
background-color: {{navBackgroundColor}}; | |
color: {{textColor}}; | |
} | |
</style> | |
</head> | |
<body> | |
<nav id="themed-nav" class="navbar"> | |
Theming Support With Handlebars | |
</nav> | |
<div class="form-group"> | |
<label> Background Color </label> | |
<input id="nav-bg" value="" /> | |
</div> | |
<div class="form-group"> | |
<label> Font Color </label> | |
<input id="nav-font" value=""/> | |
</div> | |
<button id="save">Set theme</button> | |
</body> | |
</html> |
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
function setTheme(themeColors) { | |
var $styleSheet = $('#custom-theming'); | |
var source = $styleSheet.html(); | |
$styleSheet.remove(); | |
var template = Handlebars.compile(source); | |
var parsedStyle = template(themeColors); | |
var $newStyle = $('<style id="custom-theming"></style>'); | |
$('head').append($newStyle.html(parsedStyle)); | |
} | |
// Input vals | |
var buttonElement = document.getElementById('save'); | |
buttonElement.addEventListener('click', function() { | |
var navBackgroundColor = $('#nav-bg').val(); | |
var fontColor = $('#nav-font').val(); | |
var themeColors = { | |
navBackgroundColor: navBackgroundColor, | |
textColor: fontColor | |
}; | |
setTheme(themeColors); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment