Skip to content

brownbeartech/java-soy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Soy library for Java

Google Closure Templates (Soy) is a great templating system for Java that has prebuild libraries for loading and rendering templates. This library provides two main functions, loading and caching the templates from their files and serializing Java objects for use in the templates.

Soy templates

Soy templates can be simple. Each .soy file has a unique namespace and can contain multiple templates.

{namespace your.template.namespace}

/**
 * Comment
 */
{template .template}
  Hello world
{/template}

A template can take arguments like a view model.

{namespace your.template.namespace}

/**
 * Comment
 * @param paramName Comment about param
 */
{template .template}
  {$paramName}
{/template}

And templates can make local or fully qualified calls to other templates that have been bundled together.

{namespace your.template.namespace}

/**
 * Comment
 */
{template .templateA}
  {call .templateB /}
  {call your.template.namespace.templateB /}
{/template}

/**
 * Comment
 */
{template .templateB}
  Hello world
{/template}

Loading and rendering templates

Check out a seperate library, Java Resource Management, for loading resorces in Java.

FallbackResourceFetcher fetcher = ...; // https://github.com/brownbeartech/java-resources
TemplateLoader loader = () -> fetcher.findAll(p -> p.getFileName().toString.endsWith(".soy"));
SoyTemplateRenderer renderer = new SoyTemplates(loader);
String html = renderer.render("your.template.namespace.template");

An if the template expects arguments they can be provided as a map

Map<String, Object> args = new HashMap<>();
args.put("paramName", "value");
String html = renderer.render("your.template.namespace.template", args);

About

Soy library for Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors