tschuehly / spring-view-component

Server-side UI components with spring boot
MIT License
185 stars 10 forks source link

JStachio integration #24

Open tschuehly opened 4 months ago

tschuehly commented 4 months ago

It would be good to add another template language to Spring ViewComponent. JStachio seems to be the best solution for Mustache https://github.com/jstachio/jstachio

Maybe @agentgt can help with that!

agentgt commented 4 months ago

A couple of issues with me helping at the moment:

What I mean by the last one is that w/ JStachio you don't do

@RequestMapping("/home"
public String home() {
  return "views/home.mustache";
}

But rather:

@RequestMapping("/home")
@ResponseBody
public Home home() {
  return new Home(); // JStache model.
}

Alternatively one can return a View with

https://jstach.io/doc/jstachio/current/apidocs/io.jstach.opt.spring.webmvc/io/jstach/opt/spring/webmvc/JStachioModelView.html

Thus a custom JStachioModelView might be needed. I'm fairly sure this part will not be a problem but worth mentioning.

tschuehly commented 4 months ago

Thanks for your comment, @agentgt!

Spring ViewComponent is not bound in any way to Kotlin and is natively usable in Java. For Kotlin compatibility the user can use JStachio with kapt.

The only thing that is needed is to configure JStachio to process the ViewComponent templates and then resolve them based on the expected output path.

There is a MethodReturnValueHandler that puts the template path as view and the ViewContext record into the ModelAndViewContainer. The template is automatically put into the IViewContext by a AOP Aspect.

Then nesting needs to be handled. In JTE it's pretty easy as the JTE ViewContext just implements the Content Interface that is natively renderable in JTE expressions: ViewContext