Using pre-compiled templates with Handlebars.js (jQuery Mobile environment)

前端 未结 5 961
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 16:30

I am struggling somewhat with pre-compilation of templates in Handlebars. My jQuery Mobile project is getting pretty big template-wise and I wish to pre-compile the template

5条回答
  •  清歌不尽
    2020-11-30 17:03

    Another great option for this is to use GruntJS. Once installed:

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

    Then inside your gruntfile.js

    grunt.initConfig({
        dirs: {
          handlebars: 'app/handlebars'
        },
        watch: {
          handlebars: {
            files: ['<%= handlebars.compile.src %>'],
            tasks: ['handlebars:compile']
          }
        },
        handlebars: {
          compile: {
            src: '<%= dirs.handlebars %>/*.handlebars',
            dest: '<%= dirs.handlebars %>/handlebars-templates.js'
          }
        }
    });
    
    
    grunt.loadNpmTasks('grunt-contrib-handlebars');
    

    Then you simply type grunt watch from your console, and grunt will automatically compile all *.handlebars files into your handlebars-templates.js file.

    Really rad way to work with handlebars.

提交回复
热议问题