1

I'm using Sprockets to compile Eco templates and serve them concatenated as one JS file.

So, for example, if i had these two templates:

template1.jst.eco:

<b>Awesome template 1</b>

template2.jst.eco:

<b>Awesome template 2</b>

And then i include them on a file using the Sprockets require directive:

# example.coffee
#= require 'templates/template1'
#= require 'templates/template2'

html = JST['templates/template1']()
moreHtml = JST['templates/template2']()

The compiled/concatenated output of example.js is (warning, long piece of generated code):

(function() {
  this.JST || (this.JST = {});
  this.JST["templates/template1"] = function(__obj) {
    if (!__obj) __obj = {};
    var __out = [], __capture = function(callback) {
      var out = __out, result;
      __out = [];
      callback.call(this);
      result = __out.join('');
      __out = out;
      return __safe(result);
    }, __sanitize = function(value) {
      if (value && value.ecoSafe) {
        return value;
      } else if (typeof value !== 'undefined' && value != null) {
        return __escape(value);
      } else {
        return '';
      }
    }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
    __safe = __obj.safe = function(value) {
      if (value && value.ecoSafe) {
        return value;
      } else {
        if (!(typeof value !== 'undefined' && value != null)) value = '';
        var result = new String(value);
        result.ecoSafe = true;
        return result;
      }
    };
    if (!__escape) {
      __escape = __obj.escape = function(value) {
        return ('' + value)
          .replace(/&/g, '&amp;')
          .replace(/</g, '&lt;')
          .replace(/>/g, '&gt;')
          .replace(/"/g, '&quot;');
      };
    }
    (function() {
      (function() {

        __out.push('<b>Awesome template 1</b>\n');

      }).call(this);

    }).call(__obj);
    __obj.safe = __objSafe, __obj.escape = __escape;
    return __out.join('');
  };
}).call(this);
(function() {
  this.JST || (this.JST = {});
  this.JST["templates/template2"] = function(__obj) {
    if (!__obj) __obj = {};
    var __out = [], __capture = function(callback) {
      var out = __out, result;
      __out = [];
      callback.call(this);
      result = __out.join('');
      __out = out;
      return __safe(result);
    }, __sanitize = function(value) {
      if (value && value.ecoSafe) {
        return value;
      } else if (typeof value !== 'undefined' && value != null) {
        return __escape(value);
      } else {
        return '';
      }
    }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
    __safe = __obj.safe = function(value) {
      if (value && value.ecoSafe) {
        return value;
      } else {
        if (!(typeof value !== 'undefined' && value != null)) value = '';
        var result = new String(value);
        result.ecoSafe = true;
        return result;
      }
    };
    if (!__escape) {
      __escape = __obj.escape = function(value) {
        return ('' + value)
          .replace(/&/g, '&amp;')
          .replace(/</g, '&lt;')
          .replace(/>/g, '&gt;')
          .replace(/"/g, '&quot;');
      };
    }
    (function() {
      (function() {

        __out.push('\n<b>Awesome template 2</b>\n');

      }).call(this);

    }).call(__obj);
    __obj.safe = __objSafe, __obj.escape = __escape;
    return __out.join('');
  };
}).call(this);
(function() {
  var html, moreHtml;

  html = JST['templates/template1']();

  moreHtml = JST['templates/template2']();

}).call(this);

This JS works fine, but the problem is that it's duplicating all the auxiliary template-rendering functions that Eco generates for each included template. On a project with a handful of templates it's not hard to imagine that the code for these auxiliary functions can outweigh the code for the actual templates.

Is there a way of configuring Srpockets/Eco so that it re-uses the auxiliary functions instead of duplicating them?

epidemian
  • 18,817
  • 3
  • 62
  • 71

0 Answers0