You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Views that are rendered by calling the [`render`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Render.html#v:render) function can also respond with JSON.
465
+
Views that are rendered by calling the [`renderHtmlOrJson`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Render.html#v:renderHtmlOrJson) function can respond with either HTML or JSON based on the request `Accept` header.
466
466
467
467
Let's say we have a normal HTML view that renders all posts for our blog app:
468
468
@@ -491,7 +491,7 @@ instance View IndexView where
491
491
|]
492
492
```
493
493
494
-
We can add a JSON output for all blog posts by defining a typed [`JsonResponse`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewSupport.html#t:View) payload and implementing [`jsonTyped`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewSupport.html#v:jsonTyped):
494
+
We can add a JSON output for all blog posts by defining a typed [`JsonResponse`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewSupport.html#t:JsonView) payload and implementing [`jsonTyped`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewSupport.html#v:jsonTyped) in a `JsonView` instance:
495
495
496
496
```haskell
497
497
{-# LANGUAGE DeriveGeneric #-}
@@ -515,6 +515,7 @@ instance View IndexView where
515
515
...
516
516
|]
517
517
518
+
instanceJsonViewIndexViewwhere
518
519
typeJsonResponseIndexView= [PostPayload]
519
520
520
521
jsonTyped IndexView { .. } =
@@ -528,6 +529,14 @@ instance View IndexView where
528
529
529
530
In the above code, [`jsonTyped`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewSupport.html#v:jsonTyped) has access to all arguments passed to the view, but returns a normal Haskell value instead of raw `Value`. IHP then turns that into JSON automatically using [`toJSON`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewPrelude.html#v:toJSON).
530
531
532
+
In the controller, use [`renderHtmlOrJson`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Render.html#v:renderHtmlOrJson) instead of `render` for actions that should serve both formats:
533
+
534
+
```haskell
535
+
action PostsAction=do
536
+
posts <- query @Post|> fetch
537
+
renderHtmlOrJson IndexView { .. }
538
+
```
539
+
531
540
The full `Index` View for our `PostsController` looks like this:
532
541
533
542
```haskell
@@ -573,6 +582,7 @@ instance View IndexView where
573
582
</div>
574
583
|]
575
584
585
+
instanceJsonViewIndexViewwhere
576
586
typeJsonResponseIndexView= [PostPayload]
577
587
578
588
jsonTyped IndexView { .. } =
@@ -593,7 +603,7 @@ renderPost post = [hsx|
593
603
|]
594
604
```
595
605
596
-
You can still override [`json`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewSupport.html#v:json) directly for backwards compatibility. The typed `JsonResponse` / `jsonTyped` style is preferred because it is also the representation used by IHP's OpenAPI support.
606
+
You can still override [`json`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewSupport.html#v:json) directly in `JsonView`for backwards compatibility. The typed `JsonResponse` / `jsonTyped` style is preferred because it is also the representation used by IHP's OpenAPI support.
0 commit comments