我正在尝试预编译我的把手模板Home.hbs,但它给我一个错误,说无法读取未定义的属性‘模板名称’。
这是索引文件
<!DOCTYPE html>
<html lang="en">
<link>
<meta charset="UTF-8">
<title>Title</title>
<link href="dist/styles/libs/bootstrap.css"/>
<link href="dist/styles/app.css"/>
</head>
<body>
<div class="wrapper">
<h1>This is the home page</h1>
</div>
</body>
<script src="dist/js/thirdparty.js"></script>
<script src="dist/js/templates.js"></script>
<script src="dist/js/main.js"></script>
</html>我正在使用gulp handlebar任务来预编译它
var config ={
handlebar: {
path: 'app/templates/pages/**/*.hbs',
dist: 'dist/js',
partials: 'app/templates/partials/**/*.hbs'
}
}
gulp.task('handlebar', function(){
gulp.src(config.handlebar.path)
.pipe(handlebars())
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'RRI.templates',
noRedeclare: true, // Avoid duplicate declarations
}))
.pipe(concat('templates.js'))
.pipe(gulp.dest(config.handlebar.dist));
});Home.hbs
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
<div class="wrapper">
<h2>Hello World!! </h2>
{{> _search}}
</div>_search.hbs
<h1>search</h1>main.js
$(document).ready(function() {
var template = Handlebars.templates.RRI.templates.Home;
var context = {title: "My New Post", body: "This is my first post!"};
var html = template(context);
$(".wrapper").append(html);
});它在main.js的第二行给了我一个错误,告诉我无法读取未定义的属性RRI。
template.js文件
this["RRI"] = this["RRI"] || {};
this["RRI"]["templates"] = this["RRI"]["templates"] || {};
this["RRI"]["templates"]["Home"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
var stack1, helper, alias1=helpers.helperMissing, alias2="function", alias3=this.escapeExpression;
return "\r\n\r\n<div class=\"entry\">\r\n <h1>"
+ alias3(((helper = (helper = helpers.title || (depth0 != null ? depth0.title : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"title","hash":{},"data":data}) : helper)))
+ "</h1>\r\n <div class=\"body\">\r\n "
+ alias3(((helper = (helper = helpers.body || (depth0 != null ? depth0.body : depth0)) != null ? helper : alias1),(typeof helper === alias2 ? helper.call(depth0,{"name":"body","hash":{},"data":data}) : helper)))
+ "\r\n </div>\r\n</div>\r\n\r\n<div class=\"wrapper\">\r\n <h2>Hello World!! </h2>\r\n"
+ ((stack1 = this.invokePartial(partials._search,depth0,{"name":"_search","data":data,"indent":" ","helpers":helpers,"partials":partials})) != null ? stack1 : "")
+ "</div>\r\n";
},"usePartial":true,"useData":true});我正在使用Handlebar 3.0.1,请帮助。
发布于 2016-08-09 12:23:58
最后,这是对我起作用的,template.js中的一些小变化
var template = RRI.templates.Home;我在调试后发现了这一点,但这绝对意味着要么我做了一些奇怪的事情,要么Handlebar文档没有更新。
https://stackoverflow.com/questions/38841731
复制相似问题