-
-
Save rjz/5938713 to your computer and use it in GitHub Desktop.
Revisions
-
rjz revised this gist
Aug 3, 2013 . 1 changed file with 31 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,47 +1,62 @@ // Concat EJS templates for use in client-side application // Based on https://gist.github.com/cookrn/3001401 // // Supports features found in `_.template()` -- EJS extras not included! // var path = require('path'); module.exports = function (grunt) { 'use strict'; var ejs = function ejs (files, options) { var content, namespace, templates = {}; namespace = 'window.' + options.namespace; content = namespace + ' = _.extend({}, ' + namespace + ', '; files.forEach(function (filepath) { var key, obj = templates, template = grunt.file.read(filepath); filepath = filepath.replace(options.basePath, '').split(path.sep); if (filepath[0] == '') filepath = filepath.slice(1); key = path.basename(filepath.pop(), '.ejs'); filepath.forEach(function (segment) { obj = obj[segment] = (obj[segment] || {}); }); obj[key] = template .replace(/(?:\\[ntr])+/g, ' ') .replace(/[\s\t]+/g, ' '); }); content += JSON.stringify(templates); return content + ');'; }; grunt.registerMultiTask('ejs', 'Compile ejs templates to JST file', function () { var options = this.options({ basePath: '', namespace: 'JST', requireJs: false }); this.files.forEach(function (file) { // Create JST file. var content = ejs(file.src, options); if (options.requireJs) { content = 'define([], function () {\n' + content + '\n});'; -
rjz revised this gist
Jul 6, 2013 . 5 changed files with 59 additions and 108 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ // Concat EJS templates for use in client-side application // Based on https://gist.github.com/cookrn/3001401 module.exports = function (grunt) { 'use strict'; var path = require('path'); var ejs = function (files, namespace) { var content; namespace = 'window.' + namespace; content = namespace + ' = _.extend({}, ' + namespace + ', {\n'; content += files.map(function (filepath) { var key = path.basename(filepath).replace('.ejs', ''), template = grunt.file.read(filepath); console.log( 'compiling file:' + filepath ); return '\t' + key + ': ' + JSON.stringify(template) .replace(/(?:\\[ntr])+/g, ' ') .replace(/[\s\t]+/g, ' '); }).join(',\n'); return content + '\n});'; }; grunt.registerMultiTask('ejs', 'Compile ejs templates to JST file', function () { var options = this.options({ namespace: 'JST', requireJs: false }); this.files.forEach(function (file) { var content, namespace = options.namespace; // Create JST file. content = ejs(file.src, namespace); if (options.requireJs) { content = 'define([], function () {\n' + content + '\n});'; } grunt.file.write(file.dest, content); // Fail task if errors were logged. if (grunt.errors) return false; // Otherwise, print a success message. grunt.log.writeln( 'File "' + file.dest + '" created.' ); }); }); }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,63 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +0,0 @@ -
cookrn revised this gist
Jun 29, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,5 +2,5 @@ window['JST'] = window['JST'] || {}; window['JST']['raw'] = window['JST']['raw'] || {}; window['JST']['raw']['activities/index'] = "<% _.each( activities , function( activity , id ){ %>\n <%\n var id = activity.ID\n , note = activity.Note;\n %>\n <tr>\n <td><%= id %></td>\n <td><%= note %></td>\n <td class=\"code-cell\">\n <pre><%= directive %></pre>\n </td>\n <td class=\"code-cell\">\n <pre><%= response %></pre>\n </td>\n <td><%= on_tap %></td>\n <td>\n <button\n class=\"btn btn-large btn-warning edit-activity-btn\"\n data-activity-id=\"<%= id %>\"\n data-activity-note=\"<%= note %>\"\n >*</button>\n </td>\n </tr>\n<% } ); %>\n"; window['JST']['activities/index'] = _.memoize( function( locals ){ return window.ejs.compile( window['JST']['raw']['activities/index'] )( locals ); } , function( locals ){ return _.chain( locals ).values().reduce( function( m , v ){ return m + v.toString() } , '' ).value(); } ); -
cookrn revised this gist
Jun 29, 2012 . 2 changed files with 27 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,25 @@ <!-- based on the example output, this file would be at app/views/templates/activities/index.ejs --> <% _.each( activities , function( activity , id ){ %> <% var id = activity.ID , note = activity.Note; %> <tr> <td><%= id %></td> <td><%= note %></td> <td class="code-cell"> <pre><%= directive %></pre> </td> <td class="code-cell"> <pre><%= response %></pre> </td> <td><%= on_tap %></td> <td> <button class="btn btn-large btn-warning edit-activity-btn" data-activity-id="<%= id %>" data-activity-note="<%= note %>" >*</button> </td> </tr> <% } ); %> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,6 @@ // EXAMPLE OUTPUT window['JST'] = window['JST'] || {}; window['JST']['raw'] = window['JST']['raw'] || {}; window['JST']['raw']['activities/index'] = "<% _.each( activities , function( activity , id ){ %>\n <%\n var id = activity.ID\n , note = activity.Note\n , directive = activity.Directive\n , response = activity.Response\n , on_tap = activity.OnTap;\n %>\n <tr>\n <td><%= id %></td>\n <td><%= note %></td>\n <td class=\"code-cell\">\n <pre><%= directive %></pre>\n </td>\n <td class=\"code-cell\">\n <pre><%= response %></pre>\n </td>\n <td><%= on_tap %></td>\n <td>\n <button\n class=\"btn btn-large btn-warning edit-activity-btn\"\n data-activity-id=\"<%= id %>\"\n data-activity-note=\"<%= note %>\"\n data-activity-directive=\"<%= directive %>\"\n data-activity-response=\"<%= response %>\"\n data-activity-on-tap=\"<%= on_tap %>\"\n >*</button>\n </td>\n </tr>\n<% } ); %>\n"; window['JST']['activities/index'] = _.memoize( function( locals ){ return window.ejs.compile( window['JST']['raw']['activities/index'] )( locals ); } , function( locals ){ return _.chain( locals ).values().reduce( function( m , v ){ return m + v.toString() } , '' ).value(); } ); -
cookrn revised this gist
Jun 29, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,7 +50,7 @@ module.exports = function(grunt) { , template = JSON.stringify( file.read( filepath ) ); var compile_fn = "function( locals ){ return window.ejs.compile( " + raw_namespace + "['" + key + "'] )( locals ); }" , hash_fn = "function( locals ){ return _.chain( locals ).values().reduce( function( m , v ){ return m + v.toString() } , '' ).value(); }" , template_data = ''; template_data = template_data + raw_namespace + "['" + key + "'] = " + template + ";\n"; -
cookrn revised this gist
Jun 29, 2012 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,15 +49,15 @@ module.exports = function(grunt) { var key = filepath.replace( /app\/views\/templates\// , '' ).replace( /\.ejs/ , '' ) , template = JSON.stringify( file.read( filepath ) ); var compile_fn = "function( locals ){ return window.ejs.compile( " + raw_namespace + "['" + key + "'] )( locals ); }" , hash_fn = "function( locals ){ return _.values( locals ).map( function( v ){ return v.toString() } ).join( '' ); }" , template_data = ''; template_data = template_data + raw_namespace + "['" + key + "'] = " + template + ";\n"; template_data = template_data + namespace + "['" + key + "'] = _.memoize( " + compile_fn + " , " + hash_fn + " );\n"; return template_data; } ).join( "\n\n" ) : ""; return contents; }); }; -
cookrn revised this gist
Jun 27, 2012 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,9 @@ # EJS Compiler This is used to generate a `templates.js` file where the keys are template identifiers and the values are the raw, escaped EJS templates. Templates are semi-compiled server side. Results of compilation are memoized on the client side using Underscore. This is useful for maintaining multiple template files server-side and compiling them into a client-side script. This requires the use of the `ejs` client side library. Requires `ejs` to be available on `window`. See: https://github.com/visionmedia/ejs#client-side-support -
cookrn revised this gist
Jun 27, 2012 . 3 changed files with 11 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ This is used to generate a `templates.js` file where the keys are template ident This is useful for maintaining multiple template files server-side and compiling them into a client-side script. This requires the use of the `ejs` client side library. Requires `ejs` to be available on `window`. See: https://github.com/visionmedia/ejs#client-side-support ## Resources This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,10 +36,8 @@ module.exports = function(grunt) { grunt.registerTask( 'default' , 'ejs' ); grunt.registerHelper( 'ejs' , function( files , namespace ){ namespace = "window['" + namespace + "']"; var contents = namespace + ' = ' + namespace + " || {};\n" , raw_namespace = namespace + "['raw']"; contents = contents + raw_namespace + ' = ' + raw_namespace + " || {};\n\n"; @@ -49,12 +47,14 @@ module.exports = function(grunt) { console.log( 'compiling file:' + filepath ); var key = filepath.replace( /app\/views\/templates\// , '' ).replace( /\.ejs/ , '' ) , template = JSON.stringify( file.read( filepath ) ); var compile_fn = "function( locals ){ return window.ejs.compile( " + raw_namespace + "['" + key + "'] )( locals ); }" , hash_fn = "function( locals ){ return _.values( locals ).map( function( v ){ return v.toString() } ).join( '' ); }" var template_data = ''; template_data = template_data + raw_namespace + "['" + key + "'] = " + template + ";\n"; template_data = template_data + namespace + "['" + key + "'] = _.memoize( " + compile_fn + " , " + hash_fn + " );\n"; return template_data; } ).join( "\n\n" ) : ""; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,7 @@ // EXAMPLE OUTPUT window['JST'] = window['JST'] || {}; window['JST']['raw'] = window['JST']['raw'] || {}; window['JST']['raw']['activities/edit'] = "<h1><%= hi %></h1>\n<p class=\"wut-wut\">there</p>\n"; window['JST']['activities/edit'] = _.memoize( function( locals ){ return window.ejs.compile( window['JST']['raw']['activities/edit'] )( locals ); } , function( locals ){ return _.values( locals ).map( function( v ){ return v.toString() } ).join( '' ); } ); -
cookrn revised this gist
Jun 27, 2012 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ <!-- based on the example output, this file would be at app/views/templates/activities/edit.ejs --> <h1><%= hi %></h1> <p class="wut-wut">there</p> -
cookrn revised this gist
Jun 27, 2012 . 3 changed files with 17 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,4 +8,5 @@ This requires the use of the `ejs` client side library. See: https://github.com/ ## Resources * https://gist.github.com/2877717 * https://github.com/visionmedia/ejs/issues/36 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,18 +37,25 @@ module.exports = function(grunt) { grunt.registerHelper( 'ejs' , function( files , namespace ){ namespace = "this['" + namespace + "']"; var ejs = require( 'ejs' ); // Comes out looking like this["JST"] = this["JST"] || {}; var contents = namespace + ' = ' + namespace + " || {};\n" , raw_namespace = namespace + "['raw']"; contents = contents + raw_namespace + ' = ' + raw_namespace + " || {};\n\n"; // Compile the template and get the function source contents += files ? files.map( function( filepath ){ console.log( 'compiling file:' + filepath ); var key = filepath.replace( /app\/views\/templates\// , '' ).replace( /\.ejs/ , '' ) , template = JSON.stringify( file.read( filepath ) ) , template_function = ejs.compile( template ).toString(); var template_data = ''; template_data = template_data + raw_namespace + "['" + key + "'] = " + template + ";\n"; template_data = template_data + namespace + "['" + key + "'] = function( ejs ){ return " + template_function + " };\n"; return template_data; } ).join( "\n\n" ) : ""; return contents; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,9 @@ // EXAMPLE OUTPUT this['JST'] = this['JST'] || {}; this['JST']['raw'] = this['JST']['raw'] || {}; this['JST']['raw']['activities/edit'] = "<h1><%= hi %></h1>\n<p class=\"wut-wut\">there</p>\n"; this['JST']['activities/edit'] = function( ejs ){ return function (locals){ return fn.call(this, locals, filters, utils.escape); } }; -
cookrn revised this gist
Jun 27, 2012 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,4 +4,8 @@ This is used to generate a `templates.js` file where the keys are template ident This is useful for maintaining multiple template files server-side and compiling them into a client-side script. This requires the use of the `ejs` client side library. See: https://github.com/visionmedia/ejs#client-side-support ## Resources * https://gist.github.com/2877717 -
cookrn revised this gist
Jun 27, 2012 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ // EXAMPLE OUTPUT this['JST'] = this['JST'] || {}; this['JST']['activities/edit'] = "<h1><%= hi %></h1>\n<p class=\"wut-wut\">there</p>\n"; -
cookrn created this gist
Jun 27, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ # EJS Raw Compiler This is used to generate a `templates.js` file where the keys are template identifiers and the values are the raw, escaped EJS templates. This is useful for maintaining multiple template files server-side and compiling them into a client-side script. This requires the use of the `ejs` client side library. See: https://github.com/visionmedia/ejs#client-side-support This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ module.exports = function(grunt) { var config = grunt.config , file = grunt.file , log = grunt.log; grunt.initConfig( { 'ejs': { 'app/assets/js/templates.js': 'app/views/templates/**/*.ejs' } , 'watch': { 'files': 'app/views/templates/**/*.ejs' , 'tasks': 'ejs' } } ); grunt.registerMultiTask( 'ejs' , 'Compile ejs templates to JST file' , function(){ // If namespace is specified use that, otherwise fallback var namespace = config( 'meta.ejs.namespace' ) || 'JST'; console.log(namespace); // Create JST file. var files = file.expand( this.data ); file.write( this.target , grunt.helper( 'ejs' , files , namespace ) ); // Fail task if errors were logged. if ( grunt.errors ) { return false; } // Otherwise, print a success message. log.writeln( 'File "' + this.target + '" created.' ); }); grunt.registerTask( 'default' , 'ejs' ); grunt.registerHelper( 'ejs' , function( files , namespace ){ namespace = "this['" + namespace + "']"; // Comes out looking like this["JST"] = this["JST"] || {}; var contents = namespace + " = " + namespace + " || {};\n\n"; // Compile the template and get the function source contents += files ? files.map( function( filepath ){ console.log( 'compiling file:' + filepath ); var key = filepath.replace( /app\/views\/templates\// , '' ).replace( /\.ejs/ , '' ) , template = JSON.stringify( file.read( filepath ) ); return namespace + "['" + key + "'] = " + template + ";"; } ).join( "\n\n" ) : ""; return contents; }); };