当handlebars 在 NodeJS 环境中编译模板时,我想缩小模板。
我搜索了这个,发现handlebars.compile(templateLoaded);
有更多的参数传递给它,而不仅仅是传递给编译器的模板。我想知道如何传递函数的 minify 选项以及我可以传递的其他选项。
这里是一个指向 Handlebars 代码的链接,可以实现这一点: https://github.com/wycats/handlebars.js/blob/271106d43fae96fc1287898568d000b871f19084/lib/handlebars/compiler/javascript-compiler.js
注意第 46 行和 48 行。
最佳答案
我建议使用以下资源:http://www.adamwadeharris.com/how-to-precompile-handlebars-templates/
你可以通过它(如果你有通过npm -g install handbars
使用的cli工具)-m标志来最小化,所以它会是这样的:handlebars -m js/模板/js/templates/templates.js
然后代替:
var source = $("#handlebarsScriptId").html();
var template = Handlebars.compile(source);
你通过了:
var template = Handlebars.templates.handlebarsScriptId;
并且您需要在 html 中包含:
<script src="js/handlebars.js"></script>
<script src="js/templates/templates.js"></script>
关于javascript - 带选项参数的 Handlebars 编译函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24878242/