| 1 |
<!DOCTYPE html> |
| 2 |
<!-- |
| 3 |
This example demonstrates how to compile EJS templates in the browser |
| 4 |
from a <textarea>. |
| 5 |
|
| 6 |
To use this example, first run `jake build` in the EJS root directory to |
| 7 |
produce the client-side ejs.js and ejs.min.js. Alternatively, you can |
| 8 |
download the prebuilt client scripts (both minified and not) in GitHub |
| 9 |
releases. |
| 10 |
--> |
| 11 |
<html> |
| 12 |
<head> |
| 13 |
<title>EJS compilation demo</title> |
| 14 |
<style> |
| 15 |
* { |
| 16 |
box-sizing: border-box; |
| 17 |
} |
| 18 |
/* Preformatted <textarea> */ |
| 19 |
textarea.pre { |
| 20 |
font-family: monospace; |
| 21 |
font-size: 1em; |
| 22 |
height: auto; |
| 23 |
width: 100%; |
| 24 |
} |
| 25 |
pre { |
| 26 |
border: 2px solid gray; |
| 27 |
border-radius: 2px; |
| 28 |
padding: 8px; |
| 29 |
max-height: 600px; |
| 30 |
min-height: 120px; |
| 31 |
overflow: auto; |
| 32 |
width: auto; |
| 33 |
} |
| 34 |
/* Prettier button stolen from Bootstrap */ |
| 35 |
button { |
| 36 |
padding: 8px 16px; |
| 37 |
font-size: 19px; |
| 38 |
border: 1px solid transparent; |
| 39 |
border-radius: 5px; |
| 40 |
background-color: #fff; |
| 41 |
border-color: #ccc; |
| 42 |
cursor: pointer; |
| 43 |
} |
| 44 |
button:hover, |
| 45 |
button:focus, |
| 46 |
button:active { |
| 47 |
color: #333; |
| 48 |
background-color: #e6e6e6; |
| 49 |
border-color: #adadad; |
| 50 |
} |
| 51 |
</style> |
| 52 |
</head> |
| 53 |
<body> |
| 54 |
<h1> |
| 55 |
<button type="submit" onclick="comp()"> |
| 56 |
Submit |
| 57 |
</button> |
| 58 |
</h1> |
| 59 |
|
| 60 |
<h2>Input template</h2> |
| 61 |
<textarea rows="8" class="pre" id="input"></textarea> |
| 62 |
|
| 63 |
<h2>Compilation options (JSON)</h2> |
| 64 |
<textarea rows="8" class="pre" id="options"></textarea> |
| 65 |
|
| 66 |
<h2>Locals (JSON)</h2> |
| 67 |
<textarea rows="8" class="pre" id="locals"></textarea> |
| 68 |
|
| 69 |
<h2 id="output-title">Output</h2> |
| 70 |
<pre id="output"></pre> |
| 71 |
|
| 72 |
<script src="../ejs.js"></script> |
| 73 |
<script> |
| 74 |
function comp() { |
| 75 |
// Get the elements |
| 76 |
var // input EJS template |
| 77 |
templ = document.getElementById('input') |
| 78 |
// JSON locals |
| 79 |
, locals = document.getElementById('locals') |
| 80 |
// JSON options |
| 81 |
, options = document.getElementById('options') |
| 82 |
// output text area |
| 83 |
, output = document.getElementById('output') |
| 84 |
// for dynamic output heading |
| 85 |
, outputTitle = document.getElementById('output-title') |
| 86 |
, fn; |
| 87 |
|
| 88 |
// Handle errors by catching it, print the message to the output HTML |
| 89 |
// box, and throwing it again so that it's visible on the JS console. |
| 90 |
|
| 91 |
// `stage` sets the corresponding error context. |
| 92 |
function handleError(func, stage) { |
| 93 |
try { |
| 94 |
// Try executing callback |
| 95 |
func(); |
| 96 |
} catch (e) { |
| 97 |
// If there is an exception: |
| 98 |
// 1. Change output heading to "Error when ..." |
| 99 |
outputTitle.innerHTML = 'Error ' + stage; |
| 100 |
// 2. Put the stack trace into the output text area |
| 101 |
output.textContent = e.stack || e.message || e.toString(); |
| 102 |
// 3. Rethrow it |
| 103 |
throw e; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// Parse options as JSON |
| 108 |
handleError(function () { |
| 109 |
options = JSON.parse(options.value || '{}'); |
| 110 |
}, 'when parsing options'); |
| 111 |
|
| 112 |
// Parse locals as JSON |
| 113 |
handleError(function () { |
| 114 |
locals = JSON.parse(locals.value || '{}'); |
| 115 |
}, 'when parsing locals'); |
| 116 |
|
| 117 |
// Compile template with specified template and options |
| 118 |
handleError(function () { |
| 119 |
fn = ejs.compile(templ.value, options); |
| 120 |
}, 'when compiling'); |
| 121 |
|
| 122 |
// Put HTML/client JavaScript into the output textarea |
| 123 |
if (options.client) { |
| 124 |
// Put generated JavaScript function into the text area |
| 125 |
|
| 126 |
// In EJS v3 the returned function will be in string form, so that |
| 127 |
// you don't need to `.toString()` anymore. |
| 128 |
output.textContent = fn.toString(); |
| 129 |
outputTitle.innerHTML = 'Output JS'; |
| 130 |
} |
| 131 |
else { |
| 132 |
// Put the rendered HTML into the text area |
| 133 |
handleError(function () { |
| 134 |
output.textContent = fn(locals); |
| 135 |
outputTitle.innerHTML = 'Output HTML'; |
| 136 |
}, 'when executing template function'); |
| 137 |
} |
| 138 |
} |
| 139 |
</script> |
| 140 |
</body> |
| 141 |
</html> |