胡须快递 - 为胡须创建一个部分的子目录



我在我的 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}}

有没有办法允许这样做?

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

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

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

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

现在,您可以根据需要使用部件。

最新更新