Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 416
U-ways Avatar asked Sep 16 '25 05:09

U-ways


1 Answers

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.

like image 122
U-ways Avatar answered Sep 19 '25 05:09

U-ways