MonkeyBonkey
MonkeyBonkey

Reputation: 47851

using ejs partials from express client side

How can I use partials with EJS client side? I am using express and I'd like to share the same templates between server-side and client-side. I've compiled the same templates to client side code but it doesn't recognize the partial function.

ReferenceError: ejs:38
    36|   <body>
    37| 
 >> 38|         <%- partial('header') %>
    39|     <div class="container">
    40|         <%- body %>
    41|     </div> <!-- /container -->

partial is not defined

Upvotes: 2

Views: 926

Answers (1)

irla
irla

Reputation: 489

I think includes (partials year ago) are not working on client side. You can still try to write implementation that supports them, but it would be quite hard. In my case I just wanted to disable them on client side. Adding this line:

source = source.replace(/<% include.+%>/g, "");

Did the trick. It is located in:

EJS.Compiler = function(source, left) {
   this.pre_cmd = ['var ___ViewO = [];'];
   this.post_cmd = new Array();
   this.source = ' ';   
   if (source != null)
   {
      if (typeof source == 'string')
      {
        source = source.replace(/\r\n/g, "\n");
        source = source.replace(/\r/g,   "\n");
        // Just ignore the includes
        source = source.replace(/<% include.+%>/g, "");
        this.source = source;
      } else if (source.innerHTML){
        this.source = source.innerHTML;
      } 

Of course it's far away from best solution but it made my template working both on server and client side. In my case I didn't need this include to be executed on client side.

Upvotes: 1

Related Questions