Skip to content

Instantly share code, notes, and snippets.

@TomoyaShibata
Created April 23, 2014 13:25
Show Gist options
  • Save TomoyaShibata/11215101 to your computer and use it in GitHub Desktop.
Save TomoyaShibata/11215101 to your computer and use it in GitHub Desktop.
Backbone.jsのtemplateをgruntを使って外部ファイル化 ref: http://qiita.com/bps_tomoya/items/fc7c27a530b8b3954419
>grunt build
Running "jst:dev" (jst) task
File jst/template.js created.
Done, without errors.
<script src="js/template.js"></script>
var exampleView = Backbone.View.Extend({
template: JST['template'] // 元のファイル名から拡張子を省いたもの
});
module.exports = function (grunt) {
var pkg = grunt.file.readJSON('package.json');
grunt.initConfig({
jst: {
options: {
processName: function (filename) {
return filename.match(/templates\/(.+)\.tmpl$/)[1];
},
processContent: function (src) {
return src.replace(/(^\s+|\s+$)/gm, '');
}
},
dev: {
files: {
// 出力先、及びファイル名: [元ファイルのパターン。例では分かりやすくtmpl形式を対象としている]
'jst/template.js': ['templates/**/*.tmpl']
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-jst'); // プラグインの読み込み
grunt.registerTask('build', ['jst']); // タスク登録
};
<li>listName <%- listName -></li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment