Skip to content

Commit

Permalink
Add readme example of using HtmxResponse.Builder as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
checketts committed Aug 28, 2024
1 parent ef427c8 commit 53cface
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ public HtmxResponse getMainAndPartial(Model model){

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

### HtmxResponse.Builder as an argument

An `HtmxReponse.Builder` can be injected as a controller method. This creates the parameter and adds it to the model,
allowing it to be used without requiring it be the method return value. This is useful when the return value is needed for
the template.

This allows for the following usage:

```java
@GetMapping("/endpoint")
public String endpoint(HtmxResponse.Builder htmxResponse, Model model) {
htmxResponse.trigger("event1");
model.addAttribute("aField", "aValue");
return "endpointTemplate";
}
```

For example the JTE templating library supports statically typed templates and can be used like so:

```java
@GetMapping("/endpoint")
public JteModel endpoint(HtmxResponse.Builder htmxResponse) {
htmxResponse.trigger("event1");
String aField = "aValue";
return templates.endpointTemplate(aField);
}
```


### Error handlers

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

0 comments on commit 53cface

Please sign in to comment.