Skip to content

Commit

Permalink
add soy / closure-template support
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Aug 21, 2011
1 parent 1e4def2 commit 2c25904
Show file tree
Hide file tree
Showing 7 changed files with 932 additions and 4 deletions.
22 changes: 21 additions & 1 deletion README.md
@@ -1,6 +1,6 @@
# Closure Sprockets # 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 ## Integrating with Rails 3


Expand All @@ -22,6 +22,26 @@ newHeader = goog.dom.createDom('h1', {}, 'Hello world!');
goog.dom.appendChild(document.body, newHeader); 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. 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 ## Optional configuration
Expand Down
5 changes: 3 additions & 2 deletions lib/closure-sprockets.rb
@@ -1,5 +1,6 @@
require "tilt" require "tilt"


require "closure-sprockets/version" require "closure-sprockets/version"
require "closure-sprockets/processor" require "closure-sprockets/directive_processor"
require "closure-sprockets/railtie" if defined?(Rails) require "closure-sprockets/soy_processor"
require "closure-sprockets/railtie" if defined?(Rails)
Expand Up @@ -3,6 +3,7 @@ def prepare; end


def evaluate(context, locals, &block) def evaluate(context, locals, &block)
context.require_asset 'goog/base' context.require_asset 'goog/base'
context.require_asset 'soyutils'


data.lines.each do |line| data.lines.each do |line|


Expand Down
5 changes: 4 additions & 1 deletion lib/closure-sprockets/railtie.rb
@@ -1,11 +1,14 @@
module ClosureProcessor module ClosureProcessor
class Railtie < Rails::Railtie class Railtie < Rails::Engine
config.closure = ActiveSupport::OrderedOptions.new config.closure = ActiveSupport::OrderedOptions.new
config.closure.lib = 'vendor/assets/closure-library/closure' config.closure.lib = 'vendor/assets/closure-library/closure'


initializer :setup_closure do |app| initializer :setup_closure do |app|
app.assets.append_path config.closure.lib app.assets.append_path config.closure.lib
app.assets.append_path 'vendor/assets'

app.assets.register_preprocessor 'application/javascript', ClosureDependenciesProcessor app.assets.register_preprocessor 'application/javascript', ClosureDependenciesProcessor
app.assets.register_engine '.soy', SoyTemplateProcessor
end end


end end
Expand Down
23 changes: 23 additions & 0 deletions lib/closure-sprockets/soy_processor.rb
@@ -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 added lib/jar/SoyToJsSrcCompiler.jar
Binary file not shown.

0 comments on commit 2c25904

Please sign in to comment.