Skip to content

Instantly share code, notes, and snippets.

@vybs
Created January 17, 2012 02:15
Show Gist options
  • Save vybs/1624130 to your computer and use it in GitHub Desktop.
Save vybs/1624130 to your computer and use it in GitHub Desktop.
Sample code to rendering dust template using Rhino
package com.linkedin.dust.renderer;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Scriptable;
public class DustEngine
{
public static final String MODULE = DustEngine.class.getName();
private static Scriptable globalScope;
public DustEngine(InputStream dustStream)
{
try
{
Reader dustReader = new InputStreamReader(dustStream, "UTF-8");
Context dustEngineContext = Context.enter();
dustEngineContext.setOptimizationLevel(9);
try
{
globalScope = dustEngineContext.initStandardObjects();
dustEngineContext.evaluateReader(globalScope,
dustReader,
"dust-compile.js",
0,
null);
}
finally
{
Context.exit();
}
}
catch (IOException ex)
{
throw new RuntimeException(" ERROR : Unable to load dust engine resource: ", ex);
}
}
public static String compileTemplate(String name, String rawSource)
{
Context dustContext = Context.enter();
try
{
Scriptable compileScope = dustContext.newObject(globalScope);
compileScope.setParentScope(globalScope);
compileScope.put("rawSource", compileScope, rawSource);
compileScope.put("name", compileScope, name);
try
{
return (String) dustContext.evaluateString(compileScope,
"(dust.compile(rawSource, name))",
"JDustCompiler",
0,
null);
}
catch (JavaScriptException e)
{
// Fail hard on any compile time error for dust templates
throw new RuntimeException(e);
}
}
finally
{
Context.exit();
}
}
public static void loadTemplate(String name, String rawSource)
{
Context dustContext = Context.enter();
try
{
Scriptable compileScope = dustContext.newObject(globalScope);
compileScope.setParentScope(globalScope);
compileScope.put("rawSource", compileScope, rawSource);
compileScope.put("name", compileScope, name);
try
{
dustContext.evaluateString(compileScope,
"(dust.loadSource(dust.compile(rawSource, name)))",
"JDustCompiler",
0,
null);
}
catch (JavaScriptException e)
{
// Fail hard on any compile time error for dust templates
throw new RuntimeException(e);
}
}
finally
{
Context.exit();
}
}
public static void render(String name, String json, Writer writer)
{
Context dustContext = Context.enter();
Scriptable renderScope = dustContext.newObject(globalScope);
renderScope.setParentScope(globalScope);
String renderScript =
("{ dust.render( name, JSON.parse(json) , function( err, data) { if(err) { writer.write(err);} else { writer.write( data );} } ); }");
try
{
renderScope.put("writer", renderScope, writer);
renderScope.put("json", renderScope, json);
renderScope.put("name", renderScope, name);
dustContext.evaluateString(renderScope,
renderScript,
"JDustCompiler",
0,
null);
}
catch (JavaScriptException e)
{
// Fail hard on any render time error for dust templates
throw new RuntimeException(e);
}
finally
{
Context.exit();
}
}
}
@pawarkiran
Copy link

I want to use consilidate.js for rendering dust partials to logically place templates in different folders. How do I do that? Here is information on consolidate module:
http://stackoverflow.com/questions/12489739/using-dustjs-linkedin-templates-in-node-js-and-express3-x

But I would like to implement same on server side using Rhino. How do I link consolidate.js with Rhino?

@davidzchen
Copy link

@vybs Is there a Gradle plugin for linkedin-dustjs available?

@davidzchen
Copy link

Looks like there isn't. In case anyone finds this useful, I went ahead and wrote one myself: https://github.com/davidzchen/gradle-dustjs-plugin

@RameshRM
Copy link

RameshRM commented Aug 6, 2014

@vybs, is dustjs templating w/V8 is supported in java ? I dont find any links around it

@alidhra
Copy link

alidhra commented Sep 18, 2014

@vybs - Is there a jsr-223 code (ScriptEngine, ScriptEngineFactory) for Dust JS ? or are there any plans for it ?
If not, can one create the same using the direction pointed out by your sample ?

Thanks

@wuservices
Copy link

@vybs - with this solution, there's one global dust object in globalScope shared by every thread that uses DustEngine.render(), right?

If I'm rendering a large number of 'unique' templates (let's say millions), would the dust cache would get too if I used unique names? What's the best way to handle this scenario?

For any 'unique' template, I was considering always using a reused name like "UNIQUE-" + Thread.currentThread().getId() and calling loadTemplate before rendering each time, but I wasn't sure if there was a better solution since the cache might get larger with lots of threads. This seems better than creating tons of global scopes and disposing of them each time though. I did try using the same name for all threads and did observe problems due to the shared global context between threads.

This is a special use case for personalized text vs rendering pages where templates would be reused and more finite. Thanks!

EDIT: Updated strategy for threading after finding that using the same name between threads was not safe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment