Skip to content

Instantly share code, notes, and snippets.

@rjz
Forked from cookrn/_readme.md
Last active December 19, 2015 10:09
Show Gist options
  • Select an option

  • Save rjz/5938713 to your computer and use it in GitHub Desktop.

Select an option

Save rjz/5938713 to your computer and use it in GitHub Desktop.

Revisions

  1. rjz revised this gist Aug 3, 2013. 1 changed file with 31 additions and 16 deletions.
    47 changes: 31 additions & 16 deletions grunt-ejsifier.js
    Original 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 path = require('path');
    var ejs = function ejs (files, options) {

    var ejs = function (files, namespace) {
    var content,
    namespace,
    templates = {};

    var content;
    namespace = 'window.' + options.namespace;

    namespace = 'window.' + namespace;
    content = namespace + ' = _.extend({}, ' + namespace + ', ';
    files.forEach(function (filepath) {

    content = namespace + ' = _.extend({}, ' + namespace + ', {\n';
    content += files.map(function (filepath) {
    var key = path.basename(filepath).replace('.ejs', ''),
    var key,
    obj = templates,
    template = grunt.file.read(filepath);

    console.log( 'compiling file:' + filepath );

    return '\t' + key + ': ' + JSON.stringify(template)

    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, ' ');
    }).join(',\n');

    return content + '\n});';
    });

    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) {

    var content,
    namespace = options.namespace;

    // Create JST file.
    content = ejs(file.src, namespace);
    var content = ejs(file.src, options);

    if (options.requireJs) {
    content = 'define([], function () {\n' + content + '\n});';
  2. rjz revised this gist Jul 6, 2013. 5 changed files with 59 additions and 108 deletions.
    14 changes: 0 additions & 14 deletions _readme.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +0,0 @@
    # 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

    ## Resources

    * https://gist.github.com/2877717
    * https://github.com/visionmedia/ejs/issues/36
    59 changes: 59 additions & 0 deletions grunt-ejsifier.js
    Original 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.' );
    });
    });
    };
    63 changes: 0 additions & 63 deletions grunt.js
    Original file line number Diff line number Diff line change
    @@ -1,63 +0,0 @@
    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 = "window['" + namespace + "']";

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

    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";
    template_data = template_data + namespace + "['" + key + "'] = _.memoize( " + compile_fn + " , " + hash_fn + " );\n";
    return template_data;
    } ).join( "\n\n" ) : "";

    return contents;
    });
    };
    25 changes: 0 additions & 25 deletions template.ejs
    Original file line number Diff line number Diff line change
    @@ -1,25 +0,0 @@
    <!-- 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>
    <% } ); %>
    6 changes: 0 additions & 6 deletions templates.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    // 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 %>\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(); } );
  3. @cookrn 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(); } );
  4. @cookrn 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(); } );
  5. @cookrn 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";
  6. @cookrn 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;
    });
    };
    };
  7. @cookrn 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
  8. @cookrn 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( '' ); } );
  9. @cookrn 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>
  10. @cookrn 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);
    } };
  11. @cookrn 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
  12. @cookrn 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";
  13. @cookrn 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;
    });
    };