Skip to content

Commit

Permalink
Add TemplateEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman3455 committed Jan 2, 2025
1 parent 077ad68 commit 8a4b555
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import gg.jte.ContentType;
import gg.jte.TemplateEngine;
import gg.jte.resolve.ResourceCodeResolver;
import hexlet.code.repository.BaseRepository;
import io.javalin.Javalin;
import io.javalin.rendering.template.JavalinJte;
Expand Down Expand Up @@ -38,10 +41,10 @@ public static Javalin getApp() throws IOException, SQLException {

var app = Javalin.create(config -> {
config.bundledPlugins.enableDevLogging();
config.fileRenderer(new JavalinJte());
config.fileRenderer(new JavalinJte(createTemplateEngine()));
});

app.get("/", ctx -> ctx.result("Hello World"));
app.get("/", ctx -> ctx.render("index.jte"));

return app;
}
Expand All @@ -61,4 +64,11 @@ private static String readResourceFile(String fileName) throws IOException {
return reader.lines().collect(Collectors.joining("\n"));
}
}

private static TemplateEngine createTemplateEngine() {
ClassLoader classLoader = App.class.getClassLoader();
ResourceCodeResolver codeResolver = new ResourceCodeResolver("templates", classLoader);
TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html);
return templateEngine;
}
}
Empty file.
5 changes: 5 additions & 0 deletions app/src/main/resources/templates/index.jte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@template.layout.page(
content = @`
<h2>"Hello World"</h2>
`
)
17 changes: 17 additions & 0 deletions app/src/main/resources/templates/layout/page.jte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@import gg.jte.Content
@param Content content

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We"
crossorigin="anonymous">
<title>Main page</title>
</head>
<body>
${content}
</body>
</html>

0 comments on commit 8a4b555

Please sign in to comment.