Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created August 21, 2011 05:58
Show Gist options
  • Save igrigorik/1160217 to your computer and use it in GitHub Desktop.
Save igrigorik/1160217 to your computer and use it in GitHub Desktop.
Soy/closure-templates compilation via Ruby / JRuby
{namespace examples.simple}
/**
* Greets a person using "Hello" by default.
* @param name The name of the person.
* @param? greetingWord Optional greeting word to use instead of "Hello".
*/
{template .hello}
{if not $greetingWord}
Hello {$name}!
{else}
{$greetingWord} {$name}!
{/if}
{/template}
{namespace examples.simple}
/**
* Greets a person using "Hello" by default.
* @param name The name of the person.
* @param? greetingWord Optional greeting word to use instead of "Hello".
*/
{template .helloName}
{if not $greetingWord}
Hello {$name}!
{else}
{$greetingWord} {$name}!
{/if}
{/template}
/**
* Greets a person and optionally a list of other people.
* @param name The name of the person.
* @param additionalNames The additional names to greet. May be an empty list.
*/
{template .hello}
// Greet the person.
{call .helloName data="all" /}<br>
// Greet the additional people.
{foreach $additionalName in $additionalNames}
{call .helloName}
{param name: $additionalName /}
{/call}
{if not isLast($additionalName)}
<br> // break after every line except the last
{/if}
{ifempty}
No additional people to greet.
{/foreach}
{/template}
require 'rubygems'
require 'tilt'
require 'tilt/template'
require 'java'
require 'soy-20100708.jar'
java_import "com.google.template.soy.SoyFileSet"
java_import "com.google.template.soy.data.SoyMapData"
java_import "com.google.template.soy.tofu.SoyTofu"
# http://code.google.com/closure/templates/docs/helloworld_java.html
module Tilt
class SoyTemplate < Template
self.default_mime_type = 'application/javascript'
def self.engine_initialized?; true; end
def initialize_engine; end
def prepare; end
def evaluate(scope, locals, &block)
sfs = SoyFileSet::Builder.new.add(java.io.File.new(file)).build
tofu = sfs.compileToJavaObj
@output = tofu.render("examples.simple.hello", locals, nil)
end
end
end
Tilt.register Tilt::SoyTemplate, 'soy'
puts Tilt.new('simple.soy').render
puts Tilt.new('hello_name.soy').render(nil, {'name' => 'Ilya', 'greetingWord' => 'Hola'})
puts Tilt.new('hello_names.soy').render(nil, {'additionalNames' => ['John', 'Bob'], 'name' => 'Ilya', 'greetingWord' => 'Hey'})
# igrigorik { ~/Desktop/soy } > ruby jsoy_template.rb
# Hello world!
# Hola Ilya!
# Hey Ilya!<br>Hello John!<br>Hello Bob!
{namespace examples.simple}
/**
* Says hello to the world.
*/
{template .hello}
Hello world!
{/template}
require 'tilt'
require 'tilt/template'
module Tilt
class SoyTemplate < Template
self.default_mime_type = 'application/javascript'
def self.engine_initialized?; true; end
def initialize_engine; end
def prepare; end
def evaluate(scope, locals, &block)
out = file.gsub(/soy$/, 'js')
`java -jar SoyToJsSrcCompiler.jar --outputPathFormat #{out} #{file}`
@output = IO.read(out)
end
end
end
Tilt.register Tilt::SoyTemplate, 'soy'
puts Tilt.new('simple.soy').render
# igrigorik { ~/Desktop/soy } > ruby soy_template.rb
#
# // This file was automatically generated from simple.soy.
# // Please don't edit this file by hand.
#
# if (typeof examples == 'undefined') { var examples = {}; }
# if (typeof examples.simple == 'undefined') { examples.simple = {}; }
#
#
# examples.simple.hello = function(opt_data, opt_sb) {
# var output = opt_sb || new soy.StringBuilder();
# output.append('Hello world!');
# if (!opt_sb) return output.toString();
# };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment