Permalink
Browse files

add soy / closure-template support

  • Loading branch information...
igrigorik committed Aug 21, 2011
1 parent 1e4def2 commit 2c25904fd3ae8f06d7a0c905ad5d2b487899c093
View
@@ -1,6 +1,6 @@
# Closure Sprockets
-Sprockets preprocessor for Google's [Closure tools](http://code.google.com/closure/).
+Sprockets preprocessor for Google's [Closure tools](http://code.google.com/closure/) + Closure-templates (soy) compiler.
## Integrating with Rails 3
@@ -22,6 +22,26 @@ newHeader = goog.dom.createDom('h1', {}, 'Hello world!');
goog.dom.appendChild(document.body, newHeader);
```
+You can also add a `name.soy` template in your assets folder, and it will be automatically compiled to Javascript for you! Ex:
+
+```js
+/** hello.soy */
+
+{namespace examples.simple}
+
+/**
+ * Says hello to the world.
+ */
+{template .helloSoy}
+ Hello from Soy!
+{/template}
+```
+
+```js
+var soy = goog.dom.createDom('h1', {'style': 'background-color:#EEE'}, examples.simple.helloSoy());
+goog.dom.appendChild(document.body, soy);
+```
+
That's it! Point your browser at your page and you should have a hello world greeting from Google Closure, preprocessed by the Rails 3 Asset Pipeline and without any external Python dependencies or dynamic Javascript loading.
## Optional configuration
View
@@ -1,5 +1,6 @@
require "tilt"
require "closure-sprockets/version"
-require "closure-sprockets/processor"
-require "closure-sprockets/railtie" if defined?(Rails)
+require "closure-sprockets/directive_processor"
+require "closure-sprockets/soy_processor"
+require "closure-sprockets/railtie" if defined?(Rails)
@@ -3,6 +3,7 @@ def prepare; end
def evaluate(context, locals, &block)
context.require_asset 'goog/base'
+ context.require_asset 'soyutils'
data.lines.each do |line|
@@ -1,11 +1,14 @@
module ClosureProcessor
- class Railtie < Rails::Railtie
+ class Railtie < Rails::Engine
config.closure = ActiveSupport::OrderedOptions.new
config.closure.lib = 'vendor/assets/closure-library/closure'
initializer :setup_closure do |app|
app.assets.append_path config.closure.lib
+ app.assets.append_path 'vendor/assets'
+
app.assets.register_preprocessor 'application/javascript', ClosureDependenciesProcessor
+ app.assets.register_engine '.soy', SoyTemplateProcessor
end
end
@@ -0,0 +1,23 @@
+class SoyTemplateProcessor < Tilt::Template
+ COMPILER_ROOT = File.expand_path(File.dirname(__FILE__))
+ COMPILER_JAR = File.join(COMPILER_ROOT, "/../jar/SoyToJsSrcCompiler.jar")
+
+ self.default_mime_type = 'application/javascript'
+
+ def self.engine_initialized?; true; end
+ def initialize_engine; end
+ def prepare; end
+
+ def evaluate(context, locals, &block)
+ context.require_asset 'soyutils'
+
+ # not the prettiest way to do this, but it works, for now...
+ out = file.gsub(/soy$/, 'soyjs')
+ `java -jar #{COMPILER_JAR} --outputPathFormat #{out} #{file}`
+
+ @output = IO.read(out)
+ File.delete(out)
+
+ @output
+ end
+end
Binary file not shown.
Oops, something went wrong.

0 comments on commit 2c25904

Please sign in to comment.