Node.js + Express - How to get Mustache partials working?

前端 未结 3 2021
小鲜肉
小鲜肉 2021-01-18 15:35

I\'m trying to get Mustache working properly with Express, and as one can guess, I\'m having troubles.

The following line initializes Mustache nice and clean. Variab

3条回答
  •  醉酒成梦
    2021-01-18 16:00

    Using express (at least version 3) and mustache-express, You can load partials as usual using double mustaches that begin with a greater than sign.

    First consider the following is appended within our app.js file:

    /** import the module */
    
    import mustache from 'mustache-express';
    
    /** view engine setup */
    
    app.engine('mst', mustache());
    app.set('view engine', 'mst');
    app.set('views', path.join(__dirname, 'mvc/views'));
    
    /** the route where we will display the partials */
    
    app.get('/', (req, res) => {
      let view = {
        title: 'Homepage',
        // ...
      };
    
      /** we are going to use a file called template.mst for rendering */
      res.render('template', view);
    });
    

    Any double mustaches that begin with a greater than sign (i.e. {{> file}}) will be considered a partial. The file within the partial will be rendered at runtime. Consider this file to be a partial we want to insert:

    mvc/views/partial.mst

    418 | I'm a teapot

    And here is our template:

    mvc/views/template.mst

    template.mst file

    {{> partial}}

提交回复
热议问题