简体   繁体   中英

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

I am using mustache ( mustache-express ) in my Node.js application for my view, and I decided to create a folder for my partials in my view directory like so:

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

Now whenever I request a partial, it should look for that partial at the partials directory:

<!-- should render the header partial -->
{{>header}}

<h1> {{title}} </h1>
<h3> {{message}} </h3>
<p>Welcome to {{title}}</p>

<!-- should render the footer partial -->
{{>footer}}

Is there a method to allow doing so?

In the README file for mustache-express, there is a section about parameters that states:

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

So you can configure your view engine while passing the following parameters:

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

Now you can use your partials like needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM