grunt-contrib-handlebars configuration issue

白昼怎懂夜的黑 提交于 2019-12-25 02:34:38

问题


I have an issue with the configuration of "grunt-contrib-handlebars", below is my setup at the moment.

handlebars: {
    compile: {
        options: {
            namespace: "my.namespace"
        },
        files: {
            "<%= dist %>/templates/templates.js": "<%= src %>/templates/*.handlebars"
        }
    }
}

This is what I get at the moment:

my.namespace["src/templates/baseTemplate.handlebars"]

This is what I want:

my.namespace["baseTemplate"]

So I can call the templates like this:

my.namespace.baseTemplate(templateObj);

Is it possible to get this?


回答1:


Take a look my solution

I implemented it with browserify.

  1. I generated a .js file that contains all my templates.
  2. Than I required it with browserify. Example:

var templates = require('templates/all_templates_in_this_js_file_without_extension')();

3.And now I can use it like backbonejs template

Backbone.View.extend({
 template: templates.product, //or templates['product']
 render: function(){
   this.$el.html(this.template(this.model.toJSON()))
 }
});


来源:https://stackoverflow.com/questions/22511292/grunt-contrib-handlebars-configuration-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!