如何预编译handlebars.js的局部变量? [英] How do I precompile partials for handlebars.js?

查看:109
本文介绍了如何预编译handlebars.js的局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用handlebars.js,我想开始对所有内容进行预编译,但是我似乎找不到找到预编译局部函数的方法.我的大部分模板实际上都是局部的.我尝试将它们像常规模板一样对待,但随后将它们作为部分模板调用是行不通的.

I'm using handlebars.js and I want to start precompiling everything, but I can't seem to find a way to precompile the partials. The majority of my templates are actually partials. I tried just treating my them like regular templates, but then calling them as a partial doesn't work.

有什么方法可以预编译局部文件,或者从另一个模板中调用一个模板?

Is there any way to precompile partials, or, alternatively, call one template from within another template?

推荐答案

我找到了一个更好的方法:将所有的部分作为模板进行预编译,然后在代码中使用它们之前,添加以下行:

I found an even better method: precompile all your partials as templates, then right before you use them in your code, add this line:

Handlebars.partials = Handlebars.templates;

改进之处在于1)较短,并且2)不会丢失调用父模板时可能传递的任何自定义帮助程序.

The improvements are 1) it's shorter and 2) it doesn't loose any custom helpers you might pass in when calling the parent template.

这篇关于如何预编译handlebars.js的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆