1

我正在使用 handlebars.js 为我们的单页应用程序呈现客户端模板(在 node.js 服务器端使用 Jade)。

我想在服务器端预编译车把模板并将它们捆绑为一个 JS 文件发送给客户端。

目前,我handlebars用来编译模板,如下所示:

$ handlebars template1.handlebars template2.handlebars -f precompiled_templates.js

我想编写一个 bash 脚本,它可以读取*.handlebars目录中的所有文件,然后通过把手编译器运行它们。所以,如果我有这样的目录:

templates/
  temp1.handlebars
  temp2.handlebars
  temp3.handlebars
  temp4.handlebars

在模板目录中运行我的 bash 脚本(或一行命令)实际上将运行以下handlebars命令:

$ handlebars temp1.handlebars temp2.handlebars temp3.handlebars temp4.handlebars -f precompiled_templates.js

有谁知道我如何编写一个 bash 脚本来将目录中的所有车把文件放到上面的命令行中?

4

3 回答 3

2

看起来你想要类似的东西

handlebars templates/*.handlebars -f precompiled_templates.js

除非您最终会在每个文件前加上“模板”。我的首选方法需要两行:

files=( templates/*.handlebars )
handlebars "${files[@]#templates/}" -f precompiled_templates.js.

第一行将所有需要的文件放在一个数组中。在第二行中,我们扩展了数组的内容,但从结果扩展中的每个元素中去除了“templates/”前缀。

于 2012-09-25T17:57:17.750 回答
1

在模板目录中执行:

handlebars `find -name *.handlebars` -f precompiled_templates.js

反引号表示它将执行该命令然后返回结果,因此实际上您在它返回的每个文件上运行把手。如上所述 find 将在当前目录和子目录中查找文件,因此请确保从正确的位置运行它。

于 2012-09-25T21:57:10.723 回答
0

在 Bash 中,可以使用双引号内的字符串创建列表:

FILES="/etc/hosts /etc/passwd"
for file in $FILES; do cat $file ; done

<cat all files>

您还可以使用 find 和 exec 命令。

man [find|exec] 获取更多信息

于 2012-09-25T17:52:59.280 回答