【问题标题】:Error when precompiling all handlebars templates files in one js file在一个js文件中预编译所有车把模板文件时出错
【发布时间】:2016-10-02 18:26:58
【问题描述】:

这是我的文件夹结构:

app
└───templates
    ├───templ1.hbs
    ├───templ2.hbs
    └───templ3.hbs

我想在一个 templ.js 文件中编译(预编译)所有 templN.hbs 车把模板文件,但是当我尝试通过控制台进行时

$ handlebars *.hbs -f templ.js

编译失败并出现此错误

(...)\AppData\Roaming\npm\node_modules\handlebars\bin\handlebars:120
    throw err;
    ^
 Error: Unable to open template file "*.hbs"
    at (...)\AppData\Roaming\npm\node_modules\handlebars\dist\cjs\precompiler.js:107:25
    at FSReqWrap.oncomplete (fs.js:82:15)

怎么了?

【问题讨论】:

  • 您确定您已将活动目录更改为正确的文件夹吗?很可能它正在呕吐,因为它正在寻找 templ1.hbs,但您实际上是在运行来自 ~/Desktop 或其他东西的命令。
  • @Titus 当我从 cmd 运行该命令时,我在 app/templates/ 文件夹中。事实上,使用$ handlebars templ1.hbs -f templ1.js 编译单个模板(例如 templ1.hbs)可以正常工作。

标签: javascript npm handlebars.js template-engine


【解决方案1】:

在对 Handlebars 及其预编译器进行了一些练习后,我发现解决方案就像我的问题一样简单。

首先最好留在根文件夹中,因此在我的情况下留在app 文件夹中。然后,要编译所有 .hbs 模板,只需将整个 ./handlebars 文件夹作为输入传递给车把预编译器并指定他应该查找的模板扩展名:

$ handlebars ./templates -f templ.js --extension "hbs"

一切正常。

更多预编译器的选项见http://handlebarsjs.com/precompilation.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多