1

如何在 EJS 客户端使用部分?我正在使用 express,我想在服务器端和客户端之间共享相同的模板。我已将相同的模板编译为客户端代码,但它无法识别部分功能。

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

partial is not defined
4

1 回答 1

1

我认为包括(部分一年前)在客户端不起作用。您仍然可以尝试编写支持它们的实现,但这会非常困难。就我而言,我只是想在客户端禁用它们。添加这一行:

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

当然,它离最佳解决方案还很远,但它使我的模板在服务器端和客户端都可以工作。就我而言,我不需要在客户端执行此包含。

于 2013-10-04T19:56:49.653 回答