2012-07-06 75 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; 
     } 

當然,它是遠離最佳解決方案,但它使我的模板在服務器和客戶端的工作。在我的情況下,我不需要這些包括在客戶端執行。