Skip to content

Commit

Permalink
Merge pull request #4188 from cwisniew/feature-handlbars-helpers
Browse files Browse the repository at this point in the history
Add handlebars helpers for statsheets
  • Loading branch information
cwisniew authored Jul 3, 2023
2 parents 0e4776b + 5964e20 commit 3a7de88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,7 @@ dependencies {
implementation 'com.miglayout:miglayout-swing:11.1'

implementation 'com.github.jknack:handlebars:4.3.1'


implementation 'com.github.jknack:handlebars-helpers:4.3.1'
}


Expand Down
38 changes: 33 additions & 5 deletions src/main/java/net/rptools/maptool/util/HandlebarsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.helper.AssignHelper;
import com.github.jknack.handlebars.helper.ConditionalHelpers;
import com.github.jknack.handlebars.helper.NumberHelper;
import com.github.jknack.handlebars.helper.StringHelpers;
import java.io.IOException;
import java.util.Arrays;
import net.rptools.maptool.model.Token;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* Utility class to apply a Handlebars template given a bean.
Expand All @@ -30,15 +38,29 @@ public class HandlebarsUtil<T> {
/** The compiled template. */
private final Template template;

/** Logging class instance. */
private static final Logger log = LogManager.getLogger(Token.class);

/**
* Creates a new instance of the utility class.
*
* @param stringTemplate The template to compile.
* @throws IOException If there is an error compiling the template.
*/
public HandlebarsUtil(String stringTemplate) throws IOException {
Handlebars handlebars = new Handlebars();
template = handlebars.compileInline(stringTemplate);
try {
Handlebars handlebars = new Handlebars();
StringHelpers.register(handlebars);
Arrays.stream(ConditionalHelpers.values())
.forEach(h -> handlebars.registerHelper(h.name(), h));
NumberHelper.register(handlebars);
handlebars.registerHelper(AssignHelper.NAME, AssignHelper.INSTANCE);

template = handlebars.compileInline(stringTemplate);
} catch (IOException e) {
log.error("Handlebars Error: {}", e.getMessage());
throw e;
}
}

/**
Expand All @@ -49,8 +71,14 @@ public HandlebarsUtil(String stringTemplate) throws IOException {
* @throws IOException If there is an error applying the template.
*/
public String apply(T bean) throws IOException {
Handlebars handlebars = new Handlebars();
var context = Context.newBuilder(bean).resolver(JavaBeanValueResolver.INSTANCE).build();
return template.apply(context);
try {

Handlebars handlebars = new Handlebars();
var context = Context.newBuilder(bean).resolver(JavaBeanValueResolver.INSTANCE).build();
return template.apply(context);
} catch (IOException e) {
log.error("Handlebars Error: {}", e.getMessage());
throw e;
}
}
}

0 comments on commit 3a7de88

Please sign in to comment.