2012-07-06 66 views
1

如何在EJS客户端使用partials?我使用快递,我想在服务器端和客户端之间共享相同的模板。我已经将相同的模板编译到客户端代码,但它不能识别部分函数。从快递客户端使用ejs partials

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

partial is not defined 

回答

1

我觉得包括(partials一年前)不在客户端工作。您仍然可以尝试编写支持它们的实现,但这将非常困难。在我的情况下,我只是想在客户端禁用它们。添加此行:

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

窍门。它位于:

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; 
     } 

当然,它是远离最佳解决方案,但它使我的模板在服务器和客户端的工作。在我的情况下,我不需要这些包括在客户端执行。