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:
/** 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);
Now you can use your partials like needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With