4

On the client, I would like to be able to do something similar to the below :

public_html/index.html

<html>
   <head>
      <script src="ejs-or-similar.js"></script>
   </head>
   <body>
      <div id="some-partial"></div>
      <script>
         var partialHTML = Magic.render('partials/some-partial.ejs');
         document.getElementById('some-partial').innerHTML = partialHTML;
      </script>
   </body>
</html>

public_html/partials/some-partial.ejs

<div>Hi, I come from a different file!</div>

Is it possible ?

If yes, which EJS file/documentation should I use :

http://ejs.co/
http://www.embeddedjs.com/
https://github.com/tj/ejs

Please note that I'm not interested in a discussion as to whether client-side templating is a good or bad idea, etc... Just saving everyone some precious time in advance :-)

Kawd
  • 4,122
  • 10
  • 37
  • 68

1 Answers1

1

EJS will allow you to render out templates on the client side, but you will need to provide it the template string directly and not just the file path as it does not support XHR.

ejs.co (github.com/mde/ejs) is version 2 of EJS and is more actively supported.

mike-schultz
  • 2,354
  • 10
  • 13
  • So in other words I can't really use partials on the client. Providing the template string instead sounds really ugly, more like a hack if anything. I can't imagine writing the whole HTML of my partial as a string between quotes. I could do that without using EJS to be honest, with a simple Js function I could write. Anyway, thanks for clarifying! – Kawd Feb 24 '16 at 22:55