代码之家  ›  专栏  ›  技术社区  ›  u-ways

mustache express-为mustache创建分区子目录

  •  1
  • u-ways  · 技术社区  · 7 年前

    我在用胡子( mustache-express )在node.js的视图应用程序中,我决定在视图目录中为我的部分创建一个文件夹,如下所示:

    view
      ├── partials
      │   ├── footer.mst
      │   └── header.mst
      ├── error.mst
      └── index.mst
    

    现在,每当我请求一个分部时,它应该在分部目录中查找该分部:

    <!-- should render the header partial -->
    {{>header}}
    
    <h1> {{title}} </h1>
    <h3> {{message}} </h3>
    <p>Welcome to {{title}}</p>
    
    <!-- should render the footer partial -->
    {{>footer}}
    

    是否有方法允许这样做?

    1 回复  |  直到 7 年前
        1
  •  0
  •   u-ways    7 年前

    在Mustache Express的自述文件中,有一节关于 parameters 声明如下:

    mustacheExpress方法可以采用两个参数:部分目录和部分扩展。

    因此,您可以在传递以下参数的同时配置视图引擎:

    /** The path for your view directory */
    const VIEWS_PATH = path.join(__dirname, '/views');
    
    /**
     * Pass the path for your partial directory and
     * the extension of the partials within the mustache-express method
     */
    app.engine('mst', mustache(VIEWS_PATH + '/partials', '.mst'));
    
    /** View engine setup */
    app.set('view engine', 'mst');
    app.set('views', VIEWS_PATH);
    

    现在你可以根据需要使用你的部分。