From 39e9519fffd7635001238fa508ff550ae98011f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bolila?= Date: Mon, 23 Sep 2013 16:24:10 +0100 Subject: [PATCH] Compiles into JavaScript functions for use in client-side (sub. grunt-eco) --- src/eco.plugin.coffee | 10 +++- test/out-expected/templates/eco.js | 60 +++++++++++++++++++++ test/out-expected/templates/index.html | 9 ++++ test/src/documents/templates/eco.js.eco | 2 + test/src/documents/templates/index.html.eco | 13 +++++ 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 test/out-expected/templates/eco.js create mode 100644 test/out-expected/templates/index.html create mode 100755 test/src/documents/templates/eco.js.eco create mode 100644 test/src/documents/templates/index.html.eco diff --git a/src/eco.plugin.coffee b/src/eco.plugin.coffee index 2729109..809f931 100755 --- a/src/eco.plugin.coffee +++ b/src/eco.plugin.coffee @@ -15,5 +15,11 @@ module.exports = (BasePlugin) -> # Requires eco = require('eco') - # Render - opts.content = eco.render(opts.content,templateData) + if opts.outExtension in ['js'] + # Compile template into JavaScript functions + output = eco.compile(opts.content,'') + output = "window.JST['#{opts.file.get('relativeOutPath')}'] = #{output}\n" + opts.content = "if (!window.JST) {\n window.JST = {};\n}\n#{output}\n" + else + # Render + opts.content = eco.render(opts.content,templateData) diff --git a/test/out-expected/templates/eco.js b/test/out-expected/templates/eco.js new file mode 100644 index 0000000..e8db435 --- /dev/null +++ b/test/out-expected/templates/eco.js @@ -0,0 +1,60 @@ +if (!window.JST) { + window.JST = {}; +} +window.JST['templates/eco.js'] = 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, '&') + .replace(//g, '>') + .replace(/"/g, '"'); + }; + } + (function() { + (function() { + + __out.push('Hello, '); + + __out.push(__sanitize(this.name)); + + __out.push('.\nI\'M SHOUTING AT YOU, '); + + __out.push(__sanitize(this.name.toUpperCase())); + + __out.push('!\n'); + + }).call(this); + + }).call(__obj); + __obj.safe = __objSafe, __obj.escape = __escape; + return __out.join(''); +} + diff --git a/test/out-expected/templates/index.html b/test/out-expected/templates/index.html new file mode 100644 index 0000000..e745f94 --- /dev/null +++ b/test/out-expected/templates/index.html @@ -0,0 +1,9 @@ +

this is THE CLIENT-SIDE TEST FOR ECO

+ +
.
+ + + \ No newline at end of file diff --git a/test/src/documents/templates/eco.js.eco b/test/src/documents/templates/eco.js.eco new file mode 100755 index 0000000..472befe --- /dev/null +++ b/test/src/documents/templates/eco.js.eco @@ -0,0 +1,2 @@ +Hello, <%= @name %>. +I'M SHOUTING AT YOU, <%= @name.toUpperCase() %>! diff --git a/test/src/documents/templates/index.html.eco b/test/src/documents/templates/index.html.eco new file mode 100644 index 0000000..4d71969 --- /dev/null +++ b/test/src/documents/templates/index.html.eco @@ -0,0 +1,13 @@ +--- +title: 'the client-side test for eco' +--- + +

this is <%= @document.title.toUpperCase() %>

+ +
.
+ + +