Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.

Commit 20c5424

Browse files
Daniel-Doschkal
authored andcommitted
Added Jtwig view engine (#180)
1 parent ad78da7 commit 20c5424

File tree

13 files changed

+434
-0
lines changed

13 files changed

+434
-0
lines changed

ext/jtwig/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>org.mvc-spec.ozark.ext</groupId>
24+
<artifactId>ozark-parent-ext</artifactId>
25+
<version>1.0.0-m04-SNAPSHOT</version>
26+
</parent>
27+
<artifactId>ozark-jtwig</artifactId>
28+
<packaging>jar</packaging>
29+
<name>Ozark Jtwig Extension</name>
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.jtwig</groupId>
33+
<artifactId>jtwig-web</artifactId>
34+
<version>5.87.0.RELEASE</version>
35+
</dependency>
36+
</dependencies>
37+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.ext.jtwig;
17+
18+
import java.io.IOException;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import javax.annotation.PostConstruct;
23+
import javax.enterprise.context.ApplicationScoped;
24+
import javax.mvc.engine.ViewEngineContext;
25+
import javax.mvc.engine.ViewEngineException;
26+
import javax.servlet.ServletException;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
import org.jtwig.web.servlet.JtwigRenderer;
31+
import org.mvcspec.ozark.engine.ViewEngineBase;
32+
33+
/**
34+
* @author Daniel Dias
35+
*/
36+
@ApplicationScoped
37+
public class JtwigViewEngine extends ViewEngineBase {
38+
39+
private JtwigRenderer jtwigRenderer;
40+
41+
@PostConstruct
42+
public void init() {
43+
jtwigRenderer = JtwigRenderer.defaultRenderer();
44+
}
45+
46+
public boolean supports(String view) {
47+
return view.endsWith(".twig.html") || view.endsWith(".twig");
48+
}
49+
50+
public void processView(ViewEngineContext context) throws ViewEngineException {
51+
52+
try {
53+
54+
Map<String, Object> model = new HashMap<>(context.getModels());
55+
model.put("request", context.getRequest(HttpServletRequest.class));
56+
57+
HttpServletRequest request = context.getRequest(HttpServletRequest.class);
58+
HttpServletResponse response = context.getResponse(HttpServletResponse.class);
59+
60+
jtwigRenderer.dispatcherFor(resolveView(context)).with(model).render(request,response);
61+
62+
} catch (ServletException | IOException e) {
63+
throw new ViewEngineException(e);
64+
}
65+
}
66+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
23+
bean-discovery-mode="all">
24+
</beans>

ext/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<module>velocity</module>
4444
<module>pebble</module>
4545
<module>groovy</module>
46+
<module>jtwig</module>
4647
</modules>
4748

4849
<dependencies>

test/jtwig/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>org.mvc-spec.ozark.test</groupId>
24+
<artifactId>ozark-parent-test</artifactId>
25+
<version>1.0.0-m04-SNAPSHOT</version>
26+
</parent>
27+
<artifactId>jtwig</artifactId>
28+
<packaging>war</packaging>
29+
<name>Ozark Jtwig Tests</name>
30+
<build>
31+
<finalName>test-jtwig</finalName>
32+
</build>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.mvc-spec.ozark.ext</groupId>
36+
<artifactId>ozark-jtwig</artifactId>
37+
<version>${project.version}</version>
38+
<scope>compile</scope>
39+
</dependency>
40+
</dependencies>
41+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.test.jtwig;
17+
18+
import javax.inject.Inject;
19+
import javax.mvc.Controller;
20+
import javax.mvc.Models;
21+
import javax.mvc.View;
22+
import javax.ws.rs.GET;
23+
import javax.ws.rs.Path;
24+
import javax.ws.rs.Produces;
25+
import javax.ws.rs.QueryParam;
26+
27+
/**
28+
* HelloController test.
29+
*
30+
* @author Daniel Dias
31+
*/
32+
@Path("hello")
33+
public class HelloController {
34+
35+
@Inject
36+
private Models models;
37+
38+
@GET
39+
@Controller
40+
@View("hello.twig.html")
41+
public void hello(@QueryParam("user") String user) {
42+
models.put("user", user);
43+
}
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.test.jtwig;
17+
18+
import javax.ws.rs.ApplicationPath;
19+
import javax.ws.rs.core.Application;
20+
import java.util.Collections;
21+
import java.util.Set;
22+
23+
/**
24+
* Class MyApplication.
25+
*
26+
* @author Daniel Dias
27+
*/
28+
@ApplicationPath("resources")
29+
public class MyApplication extends Application {
30+
31+
@Override
32+
public Set<Class<?>> getClasses() {
33+
return Collections.singleton(HelloController.class);
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
23+
bean-discovery-mode="all">
24+
</beans>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>Hello</title>
6+
<link rel="stylesheet" type="text/css" href="{{request.contextPath}}/ozark.css"/>
7+
</head>
8+
<body>
9+
<h1>Hello {{ user }}</h1>
10+
</body>
11+
</html>

test/jtwig/src/main/webapp/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello Controller using Jtwig</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<link rel="stylesheet" type="text/css" href="ozark.css">
7+
</head>
8+
<body>
9+
<h1>Hello Controller using Jtwig</h1>
10+
<ul>
11+
<li><a href="resources/hello?user=mvc">resources/hello?user=mvc</a>
12+
</ul>
13+
<br/>
14+
<a class="source" href="https://github.com/mvc-spec/ozark/blob/master/test/jtwig/src/main/java/org/mvcspec/ozark/test/jtwig/HelloController.java">Source Code</a>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)