-
Notifications
You must be signed in to change notification settings - Fork 1
Example: Rendering a Class to Soy Template
Stefan Liebenberg edited this page Apr 18, 2014
·
3 revisions
This example is also a TestCase here
src/test/resources/templates/models/StatisticsTemplate.soy
{namespace models}
/**
* @param Downloads
* @param PageViews
* @param UniqueVisitors
*/
{template .StatisticsTemplate}
<ul>
<li>Downloads: {$Downloads}</li>
<li>PageViews: {$PageViews}</li>
<li>UniqueVisitors: {$UniqueVisitors}</li>
</ul>
{/template}
src/test/java/example/models/Statistics.java
package example.models;
import slieb.soy.annotations.Soy;
@Soy
@Soy.Template("models.StatisticsTemplate")
public class Statistics {
private final Integer downloads, pageViews, uniqueVisitors;
public Statistics(Integer downloads, Integer pageViews, Integer uniqueVisitors) {
this.downloads = downloads;
this.pageViews = pageViews;
this.uniqueVisitors = uniqueVisitors;
}
@Soy.Method("Downloads")
public Integer getDownloads() {
return downloads;
}
@Soy.Method("PageViews")
public Integer getPageViews() {
return pageViews;
}
@Soy.Method("UniqueVisitors")
public Integer getUniqueVisitors() {
return uniqueVisitors;
}
}
public class ExampleApplication {
public static Renderer<Object> getRenderer() {
SoyTofu soyTofu = new SoyFileSet.Builder()
.add(getClass().getResource("/templates/models/StatisticsTemplate.soy"))
.build().compileToTofu();
return Loader.getRenderer(soyTofu);
}
public void static main(String[] args) {
Renderer<Object> renderer = getRenderer();
Statistics stats = new Statistics(10, 100, 1);
String result = renderer.render(stats);
System.out.println(result);
}
}