【问题标题】:Using precompiled handlebars templates with Marionette在 Marionette 中使用预编译的车把模板
【发布时间】:2013-11-08 15:56:47
【问题描述】:

我正在使用 Marionette 和 requirejs,我还想使用预编译的车把模板。 这是如何工作的?

这是我当前的设置:

require_main.js:

requirejs.config({
    baseUrl: "/",
    paths: {
        'text': 'vendor/javascripts/text',
        'backbone': "vendor/javascripts/backbone",
        'backbone.wreqr': "vendor/javascripts/backbone.wreqr",
        'backbone.babysitter': "vendor/javascripts/backbone.babysitter",
        'jquery': "vendor/javascripts/jquery",
        'jquery-ui': 'vendor/javascripts/jquery-ui',
        'json2': "vendor/javascripts/json2",
        'marionette': "vendor/javascripts/backbone.marionette",
        'underscore': "vendor/javascripts/underscore",
        'handlebars': "vendor/javascripts/handlebars"
    },

    shim: {
        'underscore': {
            exports: "_"
        },
        'backbone': {
            deps: ["jquery", "underscore", "json2"],
            exports: "Backbone"
        },
        'marionette': {
            deps: ["backbone"],
            exports: "Marionette"
        },
        'jquery-ui': ['jquery'],

    'handlebars': {
      exports: 'Handlebars'
    }
    }
});
require(["app"], function(MyApp){
    MyApp.start();
});

app.js:

define(['marionette', 'handlebars', 'text!compiled.handlebars'], function(Marionette, Handlebars, Template_one) {

    var MyApp = new Marionette.Application();

    MyApp.addRegions({
        mainRegion: "#main-region"
    });

    MyApp.StaticView = Marionette.ItemView.extend({
        template: Template_one(context)
    });

    MyApp.on("initialize:after", function(){
        var staticView = new MyApp.StaticView();
        MyApp.mainRegion.show(staticView);  
    });

});

在我的 app.js 中,我可以得到 evth。与未编译的模板一起工作就好了,像这样:

...
var template = Handlebars.compile(Template_one)
var html = template(context)
template: html
...

但是如何正确使用已编译的模板呢?

【问题讨论】:

    标签: requirejs handlebars.js marionette


    【解决方案1】:

    更新:仅使用 Handlebars 预编译器

    我之前提到Grunt 的原因是因为它对于LOT of things 来说非常方便。所以,在我看来,了解/了解它非常重要。

    但是您可以单独使用 Handlebars precompiler 实现完全相同的效果:

    $ handlebars templates/* -f js/precompiled.handlebars.js
    

    您仍然需要在您的 RequireJS 配置中集成 precompiled.handlebars.js,请参阅答案末尾。

    原文:与 Grunt

    为此,您需要Grunt Task Runner,它使这类事情变得容易得多。

    从现在开始,我假设以下文件夹结构:

    project/
        assets/
            js/
            templates/
    

    我还假设您的机器上安装了node.js


    安装 Grunt

    $ cd project/assets/
    $ sudo npm install grunt --save-dev
    

    安装 Handlebars 插件

    您还需要 Handlebars Grunt plugin 来预编译您的模板:

    这样安装:

    $ sudo npm install grunt-contrib-handlebars --save-dev
    

    Gruntfile.js

    注意事项

    • Gruntfile.js 是 Grunt 的配置文件
    • 它位于安装 Grunt CLI 实例的根目录
    • 它是用纯 javascript 编写的

    创建文件:

    $ touch Gruntfile.js
    

    然后复制/粘贴这个典型的Gruntfile 以完成您想要实现的目标:

    module.exports = function(grunt) {
    
      /*
       * https://github.com/gruntjs/grunt/wiki/Configuring-tasks
       */
      grunt.initConfig({
    
        "handlebars": {
          compile: {
            options: {
              amd: true
            },
            src: ["templates/**/*.html"],
            dest: "js/precompiled.handlebars.js"
          }
        }
    
      });
    
      // Requires the needed plugin
      grunt.loadNpmTasks('grunt-contrib-handlebars');
    };
    

    所有插件选项here

    运行任务

    然后,假设您有模板位于 assets/templates/,运行:

    $ grunt handlebars:compile
    

    如果一切正常,您应该可以在js/precompiled.handlebars.js 中看到您编译的模板:

    define([
    
        // Should be `handlebars.runtime.js` here
        // See: http://handlebarsjs.com/precompilation.html
        'handlebars'
    
    ], function(Handlebars) {
    
      this["JST"] = this["JST"] || {};
    
      this["JST"]["templates/template_one.html"] = Handlebars.template(function(Handlebars,depth0,helpers,partials,data) { /* ... */ });
    
      this["JST"]["templates/template_two.html"] = Handlebars.template(function(Handlebars,depth0,helpers,partials,data) { /* ... */ });
    
      //...
    
      return this["JST"];
    
    });
    

    与 RequireJS 集成

    显然,在您的 Views 中,您必须更改依赖项:

    define([
      'marionette',
      //'handlebars', /* You don't need this _here_ anymore */
      'js/compiled.handlebars'
    
    ], function(Marionette, JST) {
    
        /* edited for brevity */
    
        MyApp.StaticView = Marionette.ItemView.extend({
            template: JST["templates/template_one.html"]
        });
    
        MyApp.on("initialize:after", function(){
            var staticView = new MyApp.StaticView();
            MyApp.mainRegion.show(staticView);  
        });
    });
    

    【讨论】:

    • 我希望我能投票两次,非常有帮助!有什么方法可以简化访问模板而不是使用文件相对路径作为键?
    • 我不这么认为。但是 Handlebars 预编译器中可能有一个选项。
    • 我找到了一个名为 processName 的选项。车把:{ 编译:{ 选项:{ amd: true, processName: function(filePath) { var pieces = filePath.split("/");返回件[pieces.length - 1].split(".")[0]; } }, src: ['src/js/templates/*.handlebars'], dest: 'src/js/templates.js' } }
    • 我无法让它工作...您能否提供一些示例的要点?它一直说不能调用未定义的函数“模板”(应该是Handlebars)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多