Skip to content

Instantly share code, notes, and snippets.

@sng2c
Forked from ato/ERB.java
Created February 3, 2016 09:28
Show Gist options
  • Save sng2c/e4096b35bc29f74ed27e to your computer and use it in GitHub Desktop.
Save sng2c/e4096b35bc29f74ed27e to your computer and use it in GitHub Desktop.
JRuby
require 'erb'
require 'ostruct'
require 'java'
# Renders an ERB template against a hashmap of variables.
# template should be a Java InputStream
def render(template, variables)
context = OpenStruct.new(variables).instance_eval do
variables.each do |k, v|
instance_variable_set(k, v) if k[0] == '@'
end
def partial(partial_name, options={})
new_variables = marshal_dump.merge(options[:locals] || {})
Java::Pavo::ERB.render(partial_name, new_variables)
end
binding
end
ERB.new(template.to_io.read).result(context);
end
# Renders an ERB template wrapped in an ERB layout.
# template and layout should both be Java InputStreams
def render_with_layout(template, layout, variables)
render(layout, variables) do
render(template, variables)
end
end
package pavo;
import java.io.*;
import java.util.*;
import javax.script.*;
public class ERB {
static ScriptEngine jruby = initializeJRuby();
static ScriptEngine initializeJRuby() {
try (InputStream bootstrap = ERB.class.getResourceAsStream("bootstrap.rb")) {
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
jruby.eval(new InputStreamReader(bootstrap));
return jruby;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static InputStream openTemplate(String name) {
return ERB.class.getResourceAsStream(name + ".erb");
}
public static String render(String template, Map<String,Object> variables) throws Exception {
try (InputStream stream = openTemplate(template)) {
return (String) ((Invocable)jruby).invokeFunction("render", stream, variables);
}
}
/**
* Render an ERB template wrapped in a ERB layout.
*/
public static String render(String template, String layout, Map<String,Object> variables) throws Exception {
try (InputStream templateStream = openTemplate(template);
InputStream layoutStream = openTemplate(layout)) {
return (String) ((Invocable)jruby).invokeFunction("render_with_layout",
templateStream, layoutStream, variables);
}
}
}
Hello <%= @user %>
<%= partial("partial", :locals => {:city => "canberra"}) %>
package pavo;
import java.util.*;
import javax.ws.rs.*;
@Path("/")
public class HelloController {
@GET
public String hello() throws Exception {
Map<String,Object> variables = new HashMap<>();
variables.put("@title", "The Home Page");
variables.put("@user", "Michael");
return ERB.render("hello.erb", variables);
}
}
<html>
<head><title><%= @title %></title></head>
<body><%= yield %></body>
</html>
<p>I am a partial. You are <%= @user %> in <%= city %>.</p>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>au.gov.nla</groupId>
<artifactId>erbtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
<version>1.7.0.RC1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17.1</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>pavo</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>pavo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pavo</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment