【问题标题】:Express Handlebars Globbing快速车把 Globbing
【发布时间】:2018-01-19 22:38:56
【问题描述】:

我正在尝试设置我的 express 应用程序,以便它将我的所有组件作为部分读取。我的树看起来像:

-src/
--components/
---hero
---block

我想要让我的 express-handlebars 应用程序引擎在组件目录中查找,然后在其他目录中搜索部分内容,我尝试将其全局化,但这似乎不起作用。

这是我目前拥有的。

app.engine('.hbs', handlebars({
  extname: 'hbs',
  defaultLayout: 'index',
  layoutsDir: './src/pages',
  partialsDir: './src/components/**/'
}));

【问题讨论】:

    标签: javascript express gulp handlebars.js


    【解决方案1】:

    好的,这就是答案,似乎 express-handlebars 不允许您只对它进行 glob,但字符串或数组除外。除此之外,您还可以使用glob-fs,这将允许您全局化所有嵌套的部分。您可以在设置 express-handlebars 配置之前执行此操作。

    这样的事情会起作用。

    const express = require('express'),
          handlebars = require('express-handlebars'),
          app = express(),
          glob = require('glob-fs')({ gitignore: true });
    
    let server,
    host,
    port,
    files;
    
    files = glob.readdirSync('./src/components/**/');
    
    app.engine('.hbs', handlebars({
     extname: 'hbs',
     defaultLayout: 'index',
     layoutsDir: './src/pages',
     partialsDir: files
    }));
    

    希望这对将来的任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2014-12-19
      • 2018-06-07
      • 2018-06-21
      • 2016-07-31
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多