首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Handlebar.templates未定义

Handlebar.templates未定义
EN

Stack Overflow用户
提问于 2016-08-09 11:51:11
回答 1查看 663关注 0票数 0

我正在尝试预编译我的把手模板Home.hbs,但它给我一个错误,说无法读取未定义的属性‘模板名称’。

这是索引文件

代码语言:javascript
复制
<!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任务来预编译它

代码语言:javascript
复制
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

代码语言:javascript
复制
<div class="entry">
    <h1>{{title}}</h1>
    <div class="body">
        {{body}}
    </div>
</div>

<div class="wrapper">
    <h2>Hello World!! </h2>
    {{> _search}}
</div>

_search.hbs

代码语言:javascript
复制
 <h1>search</h1>

main.js

代码语言:javascript
复制
$(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文件

代码语言:javascript
复制
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,请帮助。

EN

回答 1

Stack Overflow用户

发布于 2016-08-09 12:23:58

最后,这是对我起作用的,template.js中的一些小变化

代码语言:javascript
复制
 var template = RRI.templates.Home;

我在调试后发现了这一点,但这绝对意味着要么我做了一些奇怪的事情,要么Handlebar文档没有更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38841731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档