From 7fac41488c9f84af49b6967d6db243ab81fb9d11 Mon Sep 17 00:00:00 2001 From: efim Date: Thu, 29 Jun 2023 15:20:59 +0000 Subject: [PATCH] feat: project to play with simpler fragments my problem was - thinking that th:fragment is not rendered but it is, and i get null poniter error, because argument is not there the object under name used in fragment should already be available, for example take out of th:each --- troubleshooting-thymeleaf-scala/.project | 0 .../.scalafmt.conf | 2 + troubleshooting-thymeleaf-scala/build.sbt | 9 ++++ .../project/build.properties | 1 + .../src/main/resources/templates/home.html | 42 +++++++++++++++++ .../src/main/scala/example/Main.scala | 4 ++ .../main/scala/example/TestTemplate1.scala | 47 +++++++++++++++++++ .../src/test/scala/example/ExampleSuite.scala | 8 ++++ 8 files changed, 113 insertions(+) create mode 100644 troubleshooting-thymeleaf-scala/.project create mode 100644 troubleshooting-thymeleaf-scala/.scalafmt.conf create mode 100644 troubleshooting-thymeleaf-scala/build.sbt create mode 100644 troubleshooting-thymeleaf-scala/project/build.properties create mode 100644 troubleshooting-thymeleaf-scala/src/main/resources/templates/home.html create mode 100644 troubleshooting-thymeleaf-scala/src/main/scala/example/Main.scala create mode 100644 troubleshooting-thymeleaf-scala/src/main/scala/example/TestTemplate1.scala create mode 100644 troubleshooting-thymeleaf-scala/src/test/scala/example/ExampleSuite.scala diff --git a/troubleshooting-thymeleaf-scala/.project b/troubleshooting-thymeleaf-scala/.project new file mode 100644 index 0000000..e69de29 diff --git a/troubleshooting-thymeleaf-scala/.scalafmt.conf b/troubleshooting-thymeleaf-scala/.scalafmt.conf new file mode 100644 index 0000000..db004c6 --- /dev/null +++ b/troubleshooting-thymeleaf-scala/.scalafmt.conf @@ -0,0 +1,2 @@ +version = "3.7.3" +runner.dialect = scala3 \ No newline at end of file diff --git a/troubleshooting-thymeleaf-scala/build.sbt b/troubleshooting-thymeleaf-scala/build.sbt new file mode 100644 index 0000000..4734092 --- /dev/null +++ b/troubleshooting-thymeleaf-scala/build.sbt @@ -0,0 +1,9 @@ + +val toolkitV = "0.1.7" +val toolkit = "org.scala-lang" %% "toolkit" % toolkitV +val toolkitTest = "org.scala-lang" %% "toolkit-test" % toolkitV + +ThisBuild / scalaVersion := "3.2.2" +libraryDependencies += toolkit +libraryDependencies += (toolkitTest % Test) +libraryDependencies += "org.thymeleaf" % "thymeleaf" % "3.1.1.RELEASE" diff --git a/troubleshooting-thymeleaf-scala/project/build.properties b/troubleshooting-thymeleaf-scala/project/build.properties new file mode 100644 index 0000000..40b3b8e --- /dev/null +++ b/troubleshooting-thymeleaf-scala/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.9.0 diff --git a/troubleshooting-thymeleaf-scala/src/main/resources/templates/home.html b/troubleshooting-thymeleaf-scala/src/main/resources/templates/home.html new file mode 100644 index 0000000..671e108 --- /dev/null +++ b/troubleshooting-thymeleaf-scala/src/main/resources/templates/home.html @@ -0,0 +1,42 @@ + + + + + Good Thymes Virtual Grocery + + + +
+
+ This is text +

+ and this should be dinamic +

+

+ and this for the person +

+

+ and this for the outer state +

+
+
+
+
+ anohter fragment +

+ here should use onevar +

+
+
+ +

here check myDiv fragment

+
+
+ +

here check secion fragment

+
+
+

Today is: 13 February 2011

+

Testing class: John Doe

+ + diff --git a/troubleshooting-thymeleaf-scala/src/main/scala/example/Main.scala b/troubleshooting-thymeleaf-scala/src/main/scala/example/Main.scala new file mode 100644 index 0000000..248e040 --- /dev/null +++ b/troubleshooting-thymeleaf-scala/src/main/scala/example/Main.scala @@ -0,0 +1,4 @@ +package example + +@main def main(args: String*): Unit = + println(s"Hello ${args.mkString}") diff --git a/troubleshooting-thymeleaf-scala/src/main/scala/example/TestTemplate1.scala b/troubleshooting-thymeleaf-scala/src/main/scala/example/TestTemplate1.scala new file mode 100644 index 0000000..c442e2b --- /dev/null +++ b/troubleshooting-thymeleaf-scala/src/main/scala/example/TestTemplate1.scala @@ -0,0 +1,47 @@ +package example + +import org.thymeleaf.context.Context +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver +import org.thymeleaf.TemplateEngine +import java.io.Writer +import java.io.PrintWriter +import java.io.File +import org.thymeleaf.exceptions.TemplateEngineException +import org.thymeleaf.templateresolver.TemplateResolution +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver +import java.text.SimpleDateFormat +import java.util.Date + +object TestTemplate extends App { + + val now = new SimpleDateFormat("dd MMMM YYYY - HH:mm").format(new Date()) + import scala.beans.BeanProperty + class Person( + @BeanProperty var firstName: String, + @BeanProperty var lastName: String + ) { + override def toString = s"Person: $firstName $lastName" + } + + val ctx = new Context() + ctx.setVariable("today", now) + ctx.setVariable("me", new Person("Efim", "Nefedov")) + + val templateResolver = new ClassLoaderTemplateResolver() + templateResolver.setPrefix("templates/"); + templateResolver.setSuffix(".html") + templateResolver.setTemplateMode("HTML5") + + val templateEngine = new TemplateEngine() + templateEngine.setTemplateResolver(templateResolver) + + templateEngine.setTemplateResolver(templateResolver) + + val writer = new PrintWriter(System.out) + val result = templateEngine.process("home", ctx) + + // VIEW RESULTS + val text = new String(result.getBytes()) + println("TEXT: " + text) + +} diff --git a/troubleshooting-thymeleaf-scala/src/test/scala/example/ExampleSuite.scala b/troubleshooting-thymeleaf-scala/src/test/scala/example/ExampleSuite.scala new file mode 100644 index 0000000..26c55ab --- /dev/null +++ b/troubleshooting-thymeleaf-scala/src/test/scala/example/ExampleSuite.scala @@ -0,0 +1,8 @@ +package example + +class ExampleSuite extends munit.FunSuite: + + test("addition") { + assert(1 + 1 == 2) + } +end ExampleSuite