Skip to content

Commit 53cface

Browse files
committed
Add readme example of using HtmxResponse.Builder as an argument
1 parent ef427c8 commit 53cface

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,35 @@ public HtmxResponse getMainAndPartial(Model model){
181181

182182
Using `ModelAndView` means that each fragment can have its own model (which is merged with the controller model before rendering).
183183

184+
### HtmxResponse.Builder as an argument
185+
186+
An `HtmxReponse.Builder` can be injected as a controller method. This creates the parameter and adds it to the model,
187+
allowing it to be used without requiring it be the method return value. This is useful when the return value is needed for
188+
the template.
189+
190+
This allows for the following usage:
191+
192+
```java
193+
@GetMapping("/endpoint")
194+
public String endpoint(HtmxResponse.Builder htmxResponse, Model model) {
195+
htmxResponse.trigger("event1");
196+
model.addAttribute("aField", "aValue");
197+
return "endpointTemplate";
198+
}
199+
```
200+
201+
For example the JTE templating library supports statically typed templates and can be used like so:
202+
203+
```java
204+
@GetMapping("/endpoint")
205+
public JteModel endpoint(HtmxResponse.Builder htmxResponse) {
206+
htmxResponse.trigger("event1");
207+
String aField = "aValue";
208+
return templates.endpointTemplate(aField);
209+
}
210+
```
211+
212+
184213
### Error handlers
185214

186215
It is possible to use `HtmxResponse` as a return type from error handlers.

0 commit comments

Comments
 (0)