Anyone using <JTE> plugin with complex templates w...
# ktor
n
Anyone using JTE plugin with complex templates with success? Not sure if this is issue with the JTE compiler plugin, Ktor or what, but with the basic version from the example everything works. However, if I try to advance the templating using bits from JTE official example I get exceptions at runtime. Continues in thread 👉
I have added a
my.package.jte.Page
data class that I'm using to wrap the arguments:
Copy code
call.respondTemplate(
   "index.kte", "page" to Page(.....)
)
and in the
index.kte
page I have
Copy code
@import my.package.jte.Page
@param id: Int
@param name: String
@param page: im.caffeinated.jte.Page /* <-- this and import added. Everything else is as in the Ktor example */
<html>
<body>
<h1>Hello, ${name}!</h1>
</body>
</html>
Now I'm getting
Copy code
Caused by: java.lang.ClassCastException: class my.package.jte.Page cannot be cast to class my.package.jte.Page (my.package.jte.Page is in unnamed module of loader io.ktor.server.engine.OverridingClassLoader$ChildURLClassLoader @3adcc812; my.package.jte.Page is in unnamed module of loader 'app')
Clearly it seems to being a classloader issue, but how can it be corrected? The resulting classes get compiled into the default
jte-classes
directory at the same level that e.g.
build
is. Having
Copy code
val codeResolver = DirectoryCodeResolver(Path.of("templates"))
val classLoader = this@configureTemplating.environment.classLoader
val classDirectory = Path.of("build/jte-classes")
val packageName = "my.package.jte"
val engine = TemplateEngine.create(
    codeResolver,
    classDirectory,
    gg.jte.ContentType.Html,
    classLoader
)
I'll be still be getting
Copy code
gg.jte.TemplateException: Failed to compile template, error at index.kte:4
		render(jteOutput, jteHtmlInterceptor, id, name, page);
/home/.../jte-classes/gg/jte/generated/ondemand/JteindexGenerated.kt:19:51
Reason: Unresolved reference: page
	at gg.jte.compiler.kotlin.KotlinClassCompiler.compile(KotlinClassCompiler.java:45)
	at gg.jte.compiler.TemplateCompiler.precompileClasses(TemplateCompiler.java:163)
	at gg.jte.compiler.TemplateCompiler.precompile(TemplateCompiler.java:143)
	at gg.jte.compiler.TemplateCompiler.load(TemplateCompiler.java:45)
	at gg.jte.TemplateEngine.lambda$resolveTemplateOnDemand$0(TemplateEngine.java:354)
	at java.base/java.util.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1908)
	at gg.jte.TemplateEngine.resolveTemplateOnDemand(TemplateEngine.java:347)
	at gg.jte.TemplateEngine.resolveTemplate(TemplateEngine.java:337)
	at gg.jte.TemplateEngine.render(TemplateEngine.java:228)
	at io.ktor.server.jte.JteKt$Jte$2.invoke$process(Jte.kt:49)
	at io.ktor.server.jte.JteKt$Jte$2.access$invoke$process(Jte.kt:43)
	at io.ktor.server.jte.JteKt$Jte$2$1$1.invokeSuspend(Jte.kt:61)
	at io.ktor.server.jte.JteKt$Jte$2$1$1.invoke(Jte.kt)
	at io.ktor.server.jte.JteKt$Jte$2$1$1.invoke(Jte.kt)
	at io.ktor.server.application.OnCallRespondContext.transformBody(KtorCallContexts.kt:86)
	at io.ktor.server.jte.JteKt$Jte$2$1.invokeSuspend(Jte.kt:60)
	at io.ktor.server.jte.JteKt$Jte$2$1.invoke(Jte.kt)
	at io.ktor.server.jte.JteKt$Jte$2$1.invoke(Jte.kt)
	at io.ktor.server.application.PluginBuilder$onDefaultPhase$1.invokeSuspend(PluginBuilder.kt:217)
	at io.ktor.server.application.PluginBuilder$onDefaultPhase$1.invoke(PluginBuilder.kt)
	at io.ktor.server.application.PluginBuilder$onDefaultPhase$1.invoke(PluginBuilder.kt)
	at io.ktor.server.application.PluginBuilder$onDefaultPhaseWithMessage$1$1$1.invokeSuspend(PluginBuilder.kt:200)
	at io.ktor.server.application.PluginBuilder$onDefaultPhaseWithMessage$1$1$1.invoke(PluginBuilder.kt)
	at io.ktor.server.application.PluginBuilder$onDefaultPhaseWithMessage$1$1$1.invoke(PluginBuilder.kt)
	at io.ktor.util.debug.ContextUtilsKt.addToContextInDebugMode(ContextUtils.kt:30)
	at io.ktor.server.application.PluginBuilder$onDefaultPhaseWithMessage$1$1.invokeSuspend(PluginBuilder.kt:196)
	at io.ktor.server.application.PluginBuilder$onDefaultPhaseWithMessage$1$1.invoke(PluginBuilder.kt)
	at io.ktor.server.application.PluginBuilder$onDefaultPhaseWithMessage$1$1.invoke(PluginBuilder.kt)
	at io.ktor.util.pipeline.DebugPipelineContext.proceedLoop(DebugPipelineContext.kt:80)
	at io.ktor.util.pipeline.DebugPipelineContext.proceed(DebugPipelineContext.kt:57)
	at io.ktor.util.pipeline.DebugPipelineContext.execute$ktor_utils(DebugPipelineContext.kt:63)
	at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
	at io.ktor.server.jte.RespondTemplateKt.respondTemplate(RespondTemplate.kt:47)
	at io.ktor.server.jte.RespondTemplateKt.respondTemplate$default(RespondTemplate.kt:28)
	at ....ApplicationKt$configureCalculators$1$1$1.invokeSuspend(application.kt:541)
when
build/jte-classes/my/package/jte/JteindexGenerated.kt
is:
Copy code
@file:Suppress("ktlint")
package my.package.jte
import my.package.jte.Page
@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER")
class JteindexGenerated {
companion object {
	@JvmField val JTE_NAME = "index.kte"
	@JvmField val JTE_LINE_INFO = intArrayOf(0,0,0,1,1,1,1,1,6,6,6,6,9)
	@JvmStatic fun render(jteOutput:gg.jte.html.HtmlTemplateOutput, jteHtmlInterceptor:gg.jte.html.HtmlInterceptor?, id:Int, name:String, page:my.package.jte.Page) {
		jteOutput.writeContent("<html>\n<body>\n<h1>Hello, ")
		jteOutput.setContext("h1", null)
		jteOutput.writeUserContent(name)
		jteOutput.writeContent("!</h1>\n</body>\n</html>\n")
	}
	@JvmStatic fun renderMap(jteOutput:gg.jte.html.HtmlTemplateOutput, jteHtmlInterceptor:gg.jte.html.HtmlInterceptor?, params:Map<String, Any?>) {
		val id = params["id"] as Int
		val name = params["name"] as String
		val page = params["page"] as my.package.jte.Page? ?: /* */
		render(jteOutput, jteHtmlInterceptor, id, name, page);
	}
}
}
a
Could you please try to disable development mode?
n
Yup, development mode off, with code copy-pasted from the example project and running via Gradle the page works just fine. Is this a known issue ATM?
With a more complex version of two pages and a
@template.x
call etc. I'm still getting resolution errors event when the development mode is off and the simple example code works
a
We have a few similar issues which are caused by auto reloading (when the development mode is on).
126 Views