Skip to content

Commit f2d97bd

Browse files
committed
minor improvements
* add .contextPath to Request * view engine now export _vname and _vpath variables (useful for debugging)
1 parent 8a75f58 commit f2d97bd

15 files changed

Lines changed: 103 additions & 16 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
import com.typesafe.config.ConfigFactory;
7+
import com.typesafe.config.ConfigValueFactory;
8+
9+
public class RequestContextPathFeature extends ServerFeature {
10+
11+
{
12+
use(ConfigFactory.empty().withValue("application.path", ConfigValueFactory.fromAnyRef("/x")));
13+
14+
get("/hello", req -> req.contextPath() + req.path());
15+
16+
get("/u/p:id", req -> req.contextPath() + req.path());
17+
18+
}
19+
20+
@Test
21+
public void requestPath() throws Exception {
22+
request()
23+
.get("/x/hello")
24+
.expect("/x/hello");
25+
}
26+
27+
@Test
28+
public void varRequestPath() throws Exception {
29+
request()
30+
.get("/x/u/p1")
31+
.expect("/x/u/p1");
32+
}
33+
34+
}

coverage-report/src/test/java/org/jooby/RequestPathFeature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ public class RequestPathFeature extends ServerFeature {
77

88
{
99

10-
get("/hello", (req, rsp) -> rsp.send(req.path()));
10+
get("/hello", req -> req.contextPath() + req.path());
1111

12-
get("/u/p:id", (req, rsp) -> rsp.send(req.path()));
12+
get("/u/p:id", req -> req.contextPath() + req.path());
1313

1414
}
1515

coverage-report/src/test/java/org/jooby/ftl/FtlFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class FtlFeature extends ServerFeature {
1616
public void freemarker() throws Exception {
1717
request()
1818
.get("/?model=jooby")
19-
.expect("<html><body>jooby</body></html>");
19+
.expect("<html>org/jooby/ftl/index.html:org/jooby/ftl/index<body>jooby</body></html>");
2020
}
2121

2222
}

coverage-report/src/test/java/org/jooby/hbs/HbsCustomFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class HbsCustomFeature extends ServerFeature {
2020
public void hbs() throws Exception {
2121
request()
2222
.get("/?model=jooby")
23-
.expect("<html><body>jooby</body></html>");
23+
.expect("<html><title>/org/jooby/hbs/index.html:index</title><body>jooby</body></html>");
2424
}
2525

2626
}

coverage-report/src/test/java/org/jooby/hbs/HbsCustomValueResolverFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Set<Entry<String, Object>> propertySet(final Object context) {
4040
public void shouldInjectHelpers() throws Exception {
4141
request()
4242
.get("/")
43-
.expect("<html><body>VR</body></html>");
43+
.expect("<html><title>VR:VR</title><body>VR</body></html>");
4444
}
4545

4646
}

coverage-report/src/test/java/org/jooby/hbs/HbsFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class HbsFeature extends ServerFeature {
1616
public void hbs() throws Exception {
1717
request()
1818
.get("/?model=jooby")
19-
.expect("<html><body>jooby</body></html>");
19+
.expect("<html><title>/org/jooby/hbs/index.html:org/jooby/hbs/index</title><body>jooby</body></html>");
2020
}
2121

2222
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<html><body>${model}</body></html>
1+
<html>${_vpath}:${_vname}<body>${model}</body></html>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<html><body>{{model}}</body></html>
1+
<html><title>{{_vpath}}:{{_vname}}</title><body>{{model}}</body></html>

jooby-ftl/src/main/java/org/jooby/internal/ftl/Engine.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public void render(final View view, final Renderer.Context ctx) throws Exception
5555

5656
Map<String, Object> hash = new HashMap<>();
5757

58+
hash.put("_vname", view.name());
59+
hash.put("_vpath", template.getName());
60+
5861
// locals
5962
hash.putAll(ctx.locals());
6063

jooby-hbs/src/main/java/org/jooby/internal/hbs/HbsEngine.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020

2121
import static java.util.Objects.requireNonNull;
2222

23+
import java.util.Map;
24+
2325
import org.jooby.MediaType;
2426
import org.jooby.Renderer;
2527
import org.jooby.View;
2628

2729
import com.github.jknack.handlebars.Handlebars;
2830
import com.github.jknack.handlebars.Template;
2931
import com.github.jknack.handlebars.ValueResolver;
32+
import com.github.jknack.handlebars.io.TemplateSource;
3033

3134
public class HbsEngine implements View.Engine {
3235

@@ -41,12 +44,18 @@ public HbsEngine(final Handlebars handlebars, final ValueResolver[] resolvers) {
4144

4245
@Override
4346
public void render(final View view, final Renderer.Context ctx) throws Exception {
44-
Template template = handlebars.compile(view.name());
47+
String vname = view.name();
48+
TemplateSource source = handlebars.getLoader().sourceAt(vname);
49+
Template template = handlebars.compile(source);
50+
51+
Map<String, Object> locals = ctx.locals();
52+
locals.putIfAbsent("_vname", vname);
53+
locals.putIfAbsent("_vpath", source.filename());
4554

4655
com.github.jknack.handlebars.Context context = com.github.jknack.handlebars.Context
4756
.newBuilder(view.model())
4857
// merge request locals (req+sessions locals)
49-
.combine(ctx.locals())
58+
.combine(locals)
5059
.resolver(resolvers)
5160
.build();
5261

0 commit comments

Comments
 (0)