- 我有 HTML 模板文件(下划线模板语法)
- 这些文件以 HTML 格式保存,因此很容易编辑(IDE 语法突出显示)
- 我不想用ajax获取它们,而是将它们全部组合起来并将它们包含为
js
file.
- 使用 GULP 作为我的任务运行程序,我希望它能以某种方式结合所有HTML 为类似这样的内容,作为我可以包含在构建过程中的 javascript 文件:
template_file_name
是 HTML 文件名。
var templates = {
template_file_name : '...template HTML string...',
template_file_name2 : '...template HTML string...',
template_file_name3 : '...template HTML string...'
}
我真的不知道如何解决这个问题,以及如何从所有文件创建这样的文本。是的,我可以将每个单独的文件转换为字符串,但如何将其放入对象中?
更新 - 2015 年 10 月 25 日 - ES6 模块:
对于那些想要将模板作为 ES6 模块的人,我创建了gulp 文件内容到模块
演示输出:
export var file_name = "This is bar.";
export var file_name2 = "This is foo.\r\n";
export var my-folder__file_name = "This is baz.\r\n";
我所有的 NPM 包都与使用 gulp 组合模板文件相关:
- https://www.npmjs.com/package/gulp-file-contents-to-keys
- https://www.npmjs.com/package/gulp-file-contents-to-modules
- https://www.npmjs.com/package/gulp-template-compile-es6
我发现了这个很棒的工具,它完全可以满足我的需求:
https://www.npmjs.org/package/gulp-template-compile
Usage (简单如):
gulp.task('templates', function () {
gulp.src('./views/templates/**/*.html')
.pipe(template()) // converts html to JS
.pipe(concat('templates.js'))
.pipe(gulp.dest('./js/dist/'))
});
然后您可以使用以下方式访问键/值对象window.JST
。这些值是函数(我不知道为什么,但就是这样)
更新 - 2015 年 8 月 21 日
我决定使用usegulp 文件内容转 json这是从文件内容生成 JSON 的最简单的方法。
更新 - 2016 年 7 月 19 日
我创建了 3 个 NPM 包(可能对某人有用):
- https://www.npmjs.com/package/gulp-file-contents-to-keys
- https://www.npmjs.com/package/gulp-file-contents-to-modules
- https://www.npmjs.com/package/gulp-template-compile-es6
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)