casid / jte

Secure and speedy templates for Java and Kotlin.
https://jte.gg
Apache License 2.0
748 stars 56 forks source link

Support Custom Policy on GenerateJteWorker #262

Closed wyaeld closed 11 months ago

wyaeld commented 1 year ago

This is related to https://github.com/casid/jte/pull/256

Runtime compiled templates are working fine.

However, we precompile templates for non-development environments using the gradle plugin, and even with both the library and plugin on version 3.0.3, this happens.

* What went wrong:
Execution failed for task ':oak-konsole:generateJte'.
> There was a failure while executing work items
   > A failure occurred while executing gg.jte.gradle.GenerateJteWorker
      > Failed to compile components/OakSelect.kte, error at line 16: Illegal HTML attribute name data-${dataControllerTarget}-target! Expressions in HTML attribute names are not allowed.

Looking at how the TemplateEngine is initiated here

https://github.com/casid/jte/blob/62159c17baabc89379992d08f8ca9a10b51dc013/jte-gradle-plugin/src/main/java/gg/jte/gradle/GenerateJteWorker.java#L26-L38

I don't see anything that allows for passing in our HotwirePolicy. Is there a way to do so that I'm not seeing. If not, could you support one?

casid commented 1 year ago

Problem is, that the generate task does not have access to project classes yet.

You could change your build to use the precompile task, which has the property htmlPolicyClass.

Since this is gradle, you could probably add a htmlPolicy property to gg.jte.gradle.GenerateJteTask. But then you would have to duplicate your policy class.

edward3h commented 1 year ago

I split my Gradle projects into sub-projects. For example, JTE is in a 'views' sub-project, your policy could be in a 'shared' sub-project that 'views' depends on, and your server code in a 'server' sub-project that depends on both.

For the data that should be passed in to JTE template rendering, I put interfaces in the 'views' project and have model classes in the 'server' project implement them.

wyaeld commented 1 year ago

Checking things before I make changes, but looking here https://github.com/casid/jte/commit/ee6ae33707ba2315f3cce4ddf40ff1ad190a1478#diff-7c5716f58e03a18d258b4117d29790f25e14f0c2ddc98cb301b2ea144189e220R39

The JteExtension class already has place for HtmlPolicy, but we've tried it out - nothing happens - and looking through the code and the recent PR from @edward3h it doesn't appear to be connected up to the actual TemplateEngine, unless I'm missing some typical gradle indirection

wyaeld commented 1 year ago

I'm a bit slow, now that I've been digging into the code and docs together with your feedback I can see what's happening. Will keep trying to get this part to work and report back here

wyaeld commented 1 year ago

@casid you can close this issue, we have it working with precompile. We did duplicate the policy class, as having the project gradle aware of the one buried in the application source code was a bit awkward for a few reasons, but that's a very minor tradeoff.

generate ends up being no use to use. What is the intended use-case there?