Debian Codegraph @

tests/inputs/sample.ejs

raw
1 <!DOCTYPE html>
2 <!--
3 This example demonstrates how to compile and render EJS templates in the
4 browser. It is usually recommended to always embed a precompiled function
5 in the HTML, rather than compile it on-the-fly, but this demonstrates that
6 it is possible.
7 To use this example, first run `jake build` in the EJS root directory to
8 produce the client-side ejs.js and ejs.min.js. Alternatively, you can
9 download the prebuilt client scripts (both minified and not) in GitHub
10 releases.
11 -->
12 <html>
13 <head>
14 <script src="../ejs.js"></script>
15 <!-- `type` can be anything but application/javascript -->
16 <script id="users" type="text/template">
17 <% if (names.length) { %>
18 <ul>
19 <% names.forEach(function(name){ %>
20 <li><%= name %></li>
21 <% }) %>
22 </ul>
23 <% } %>
24 </script>
25 <script>
26 onload = function () {
27 // get the EJS template as a string
28 var templ = document.getElementById('users').innerHTML;
29 console.log('EJS template:');
30 console.log(templ);
31 // data to output to the template function
32 var data = { names: ['loki', 'tobi', 'jane'] };
33 // stores the rendered HTML
34 var html = ejs.compile(templ)(data);
35 console.log('Rendered HTML:');
36 console.log(html);
37 // inject the rendered data to <body>
38 document.body.innerHTML = html;
39 console.log('HTML injected to <body>');
40 }
41 </script>
42 </head>
43 <body>
44 </body>
45 </html>