Skip to content

Commit ce6beb4

Browse files
committed
Add handlebars helpers
1 parent 1486180 commit ce6beb4

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ allprojects {
2121

2222
// Using Jitpack so I need the repo name in the group to match.
2323
group = 'com.stubbornjava.StubbornJava'
24-
version = '0.1.12-SNAPSHOT'
24+
version = '0.1.13-SNAPSHOT'
2525

2626
repositories {
2727
mavenLocal()

‎gradle/dependencies.gradle‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ ext {
4747
handlebars : "com.github.jknack:handlebars:$versions.handlebars",
4848
handlebarsJackson : "com.github.jknack:handlebars-jackson2:$versions.handlebars",
4949
handlebarsMarkdown : "com.github.jknack:handlebars-markdown:$versions.handlebars",
50+
handlebarsHumanize : "com.github.jknack:handlebars-humanize:$versions.handlebars",
51+
handlebarsHelpers : "com.github.jknack:handlebars-helpers:$versions.handlebars",
5052
htmlCompressor : "com.googlecode.htmlcompressor:htmlcompressor:$versions.htmlCompressor",
5153
hikaricp : "com.zaxxer:HikariCP:$versions.hikaricp",
5254
jool : "org.jooq:jool:$versions.jool",

‎stubbornjava-common/build.gradle‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ dependencies {
2121
compile libs.handlebars
2222
compile libs.handlebarsJackson
2323
compile libs.handlebarsMarkdown
24+
compile libs.handlebarsHelpers
25+
compile libs.handlebarsHumanize
2426
compile libs.htmlCompressor
2527
compile libs.hikaricp
2628
compile libs.jool

‎stubbornjava-common/src/main/java/com/stubbornjava/common/Templating.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
import java.io.IOException;
44
import java.util.List;
55
import java.util.Map;
6+
import java.util.function.Consumer;
67

78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910

1011
import com.github.jknack.handlebars.Handlebars;
1112
import com.github.jknack.handlebars.Helper;
13+
import com.github.jknack.handlebars.HumanizeHelper;
1214
import com.github.jknack.handlebars.MarkdownHelper;
1315
import com.github.jknack.handlebars.Template;
1416
import com.github.jknack.handlebars.cache.ConcurrentMapTemplateCache;
17+
import com.github.jknack.handlebars.helper.AssignHelper;
1518
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
1619
import com.github.jknack.handlebars.io.FileTemplateLoader;
1720
import com.github.jknack.handlebars.io.TemplateLoader;
@@ -27,7 +30,9 @@ public class Templating {
2730
Templating.Builder builder =
2831
new Templating.Builder()
2932
.withHelper("dateFormat", TemplateHelpers::dateFormat)
30-
.withHelper("md", new MarkdownHelper());
33+
.withHelper("md", new MarkdownHelper())
34+
.withHelper(AssignHelper.NAME, AssignHelper.INSTANCE)
35+
.register(HumanizeHelper::register);
3136
// Don't cache locally, makes development annoying
3237
if (Env.LOCAL != Env.get()) {
3338
builder.withCaching()
@@ -125,6 +130,12 @@ public <T> Builder withHelper(String helperName, Helper<T> helper) {
125130
return this;
126131
}
127132

133+
public <T> Builder register(Consumer<Handlebars> consumer) {
134+
log.debug("registering helpers");
135+
consumer.accept(handlebars);
136+
return this;
137+
}
138+
128139
public Templating build() {
129140
handlebars.with(loaders.toArray(new TemplateLoader[0]));
130141
return new Templating(handlebars);

0 commit comments

Comments
 (0)