【问题标题】:Handlebars: Organizing and accessing remote templatesHandlebars:组织和访问远程模板
【发布时间】:2014-02-10 23:12:05
【问题描述】:
我想在不同的 HTML 文件中组织我的 HB 模板。所以,我有helpers.html,其中包含<script id='alert' type='text/template>...</script> 和<script id='notification' type='text/template'></script>,那么我将如何访问此文件中的特定模板?
使用 Jquery,我们会做类似 $('#alert') 的事情,但这是一个远程模板……有可能吗?
【问题讨论】:
标签:
javascript
jquery
templates
handlebars.js
【解决方案1】:
想法:尝试使用它们,如果找不到所需的内容,请加载它。
这是一个简单的示例,但您可以通过更复杂的解决方案变得更聪明:
function useAlert(){
var el = $('#alert');
// in case el is available carry on
if(el){
// do whatever you want
} else {
// here we're loading the templates inside an element with id #templates'
// in the callback we're using the recursion to call again this method
$('#templates').load('/path/to/helpers.htm', function(error){
// debug... remove the console log in production
if(error){
return console.log('Error', error);
}
// try again now
useAlert();
});
}