mustache-express - 为 mustache 创建一个 partials 的子目录

mustache-express - create a sub-directory of partials for mustache

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

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

是否有允许这样做的方法?

在 mustache-express 的自述文件中,有一个关于 parameters 的部分指出:

The mustacheExpress method can take two parameters: the directory of the partials and the extension of the partials.

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

/** import the module */
import mustache from 'mustache-express';

/** 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);

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