Created
April 20, 2017 04:46
-
-
Save mattborn/a279d23b229858594cfe4a9cf8013324 to your computer and use it in GitHub Desktop.
Inline templates
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> | |
<title>Inline templates</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> | |
<style> | |
body { | |
font-size: 200%; | |
margin: 10%; | |
} | |
code { background: #eee; } | |
</style> | |
</head> | |
<body> | |
<div id="render"></div> | |
<p>This gist shows how to use an inline HTML <code><template></code> with <a href="https://lodash.com/docs/4.17.4#template">lodash</a> <code>_.template</code>.</p> | |
<template id="template"> | |
<h1>Hello {{ what }}!</h1> | |
</template> | |
<script src="scripts.js"></script> | |
</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
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g; | |
const x = document.createElement('div'); | |
x.append(document.getElementById('template').content); | |
const template = _.template(x.innerHTML.trim()); | |
document.getElementById('render').innerHTML = template({what: 'World'}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment