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
This commit is contained in:
efim 2023-06-29 15:20:59 +00:00
parent 2dff41f428
commit 7fac41488c
8 changed files with 113 additions and 0 deletions

View File

View File

@ -0,0 +1,2 @@
version = "3.7.3"
runner.dialect = scala3

View File

@ -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"

View File

@ -0,0 +1 @@
sbt.version=1.9.0

View File

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div th:remove="all">
<section th:fragment="mySection(dateVar, person)" >
This is text
<p th:text="${dateVar}">
and this should be dinamic
</p>
<p th:text="${person}">
and this for the person
</p>
<p th:text="${me.firstName}">
and this for the outer state
</p>
</section>
</div>
<div th:remove="all">
<div th:fragment="myDiv" >
anohter fragment
<p th:text="${onevar}">
here should use onevar
</p>
</div>
</div>
<h2>here check myDiv fragment</h2>
<div th:replace="::myDiv" th:with="onevar=${me}"></div>
<div th:replace="::myDiv (onevar=${me})"></div>
<h2>here check secion fragment</h2>
<section th:replace=":: mySection(${today}, ${me})">
</section>
<p>Today is: <span th:text="${today}">13 February 2011</span></p>
<p>Testing class: <span th:text="${me.firstName}">John Doe</span></p>
</body>
</html>

View File

@ -0,0 +1,4 @@
package example
@main def main(args: String*): Unit =
println(s"Hello ${args.mkString}")

View File

@ -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)
}

View File

@ -0,0 +1,8 @@
package example
class ExampleSuite extends munit.FunSuite:
test("addition") {
assert(1 + 1 == 2)
}
end ExampleSuite