Skip to content

Instantly share code, notes, and snippets.

@cookrn
Created June 27, 2012 04:19
Show Gist options
  • Select an option

  • Save cookrn/3001401 to your computer and use it in GitHub Desktop.

Select an option

Save cookrn/3001401 to your computer and use it in GitHub Desktop.

Revisions

  1. cookrn revised this gist Jun 29, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion templates.js
    Original 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 , 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']['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(); } );
  2. cookrn revised this gist Jun 29, 2012. 2 changed files with 27 additions and 6 deletions.
    28 changes: 25 additions & 3 deletions template.ejs
    Original 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/edit.ejs -->
    <h1><%= hi %></h1>
    <p class="wut-wut">there</p>
    <!-- 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>
    <% } ); %>
    5 changes: 2 additions & 3 deletions templates.js
    Original 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/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( '' ); } );
    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(); } );
  3. cookrn revised this gist Jun 29, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion grunt.js
    Original 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 _.values( locals ).map( function( v ){ return v.toString() } ).join( '' ); }"
    , 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";
  4. cookrn revised this gist Jun 29, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions grunt.js
    Original 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( '' ); }"
    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 = '';

    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" ) : "";

    return contents;
    });
    };
    };
  5. cookrn revised this gist Jun 27, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion _readme.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    # EJS Raw Compiler
    # 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
  6. cookrn revised this gist Jun 27, 2012. 3 changed files with 11 additions and 13 deletions.
    2 changes: 1 addition & 1 deletion _readme.md
    Original 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. See: https://github.com/visionmedia/ejs#client-side-support
    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

    12 changes: 6 additions & 6 deletions grunt.js
    Original 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 = "this['" + namespace + "']";
    var ejs = require( 'ejs' );
    namespace = "window['" + namespace + "']";

    // 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";
    @@ -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 ) )
    , template_function = ejs.compile( template ).toString();
    , 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 + "'] = function( ejs ){ return " + template_function + " };\n";
    template_data = template_data + namespace + "['" + key + "'] = _.memoize( " + compile_fn + " , " + hash_fn + " );\n";
    return template_data;
    } ).join( "\n\n" ) : "";

    10 changes: 4 additions & 6 deletions templates.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    // EXAMPLE OUTPUT

    this['JST'] = this['JST'] || {};
    this['JST']['raw'] = this['JST']['raw'] || {};
    window['JST'] = window['JST'] || {};
    window['JST']['raw'] = window['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);
    } };
    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( '' ); } );
  7. cookrn revised this gist Jun 27, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions template.ejs
    Original 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>
  8. cookrn revised this gist Jun 27, 2012. 3 changed files with 17 additions and 5 deletions.
    3 changes: 2 additions & 1 deletion _readme.md
    Original 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://gist.github.com/2877717
    * https://github.com/visionmedia/ejs/issues/36
    13 changes: 10 additions & 3 deletions grunt.js
    Original 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\n";
    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 = JSON.stringify( file.read( filepath ) )
    , template_function = ejs.compile( template ).toString();

    return namespace + "['" + key + "'] = " + template + ";";
    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;
    6 changes: 5 additions & 1 deletion templates.js
    Original 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']['activities/edit'] = "<h1><%= hi %></h1>\n<p class=\"wut-wut\">there</p>\n";
    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);
    } };
  9. cookrn revised this gist Jun 27, 2012. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion _readme.md
    Original 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
    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
  10. cookrn revised this gist Jun 27, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions templates.js
    Original 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";
  11. cookrn created this gist Jun 27, 2012.
    7 changes: 7 additions & 0 deletions _readme.md
    Original 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
    56 changes: 56 additions & 0 deletions grunt.js
    Original 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;
    });
    };