Page History
...
However, having the "proper" content type may not be required. In many use cases it is sufficient to use the FreeMarker renderType
that responds with text/html
.
Renderer configuration
Localtab Group | ||||||||
---|---|---|---|---|---|---|---|---|
Localtab | ||||||||
| ||||||||
Code Block | ||||||||
| ||||||||
!inherit:freemarker
contentType: application/json The above example requires the Magnolia specific Localtab | | |||||||
| ||||||||
Advanced Tables - Table Plus | ||||||||
| ||||||||
Node name | Value |
|
|
|
| |||
| application/json | |||||||
| ../freemarker | |||||||
| json |
Template definition
Code Pro |
---|
title: jsonprovider0815
templateScript: /ftl-json-provider/templates/pages/jsonprovider0815.ftl
renderType: json
#renderType: freemarker
visible: true |
Turning a template-based JSON provider into a RESTful API with virtualURIMapping
You can create a template that accepts request parameters to specify the JSON content to be returned. In the example below, you can specify the workspace
and the path
of the node.
Template definition
Code Pro |
---|
title: jsonprovider0815
templateScript: /ftl-json-provider/templates/pages/jsonprovider0815.ftl
renderType: json
#renderType: freemarker
visible: true |
Turning a template-based JSON provider into a RESTful API with virtualURIMapping
You can create a template that accepts request parameters to specify the JSON content to be returned. In the example below, you can specify the workspace
and the path
of the node.
Code Pro |
---|
[#assign path = ctx.path!"undefined" /]
[#assign workspace = ctx.workspace |
Code Pro |
[#assign path = ctx.path!"undefined" /]
[#assign workspace = ctx.workspace!"tours" /]
[#assign response = '{ "error" : "Missing path parameter." }' /]
[#if path!="undefined"]
[#assign contentNode = cmsfn.contentByPath(path, workspace)]
[#assign response = jsonfn.from(contentNode).add("@path", "name", "description").wrapForI18n().print() /]
[/#if]
${response} |
Create the page with the JSON-providing template at /getjson
. You can call it with the following URL:
Mgnl mini code snippet |
---|
http://localhost:8080/magnoliaAuthor/getjson? |
If you want to use a RESTful URL instead:
Mgnl mini code snippet |
---|
http://localhost:8080/magnoliaAuthor/getjson/tours /magnolia-travels/Kyoto |
Add the following virtual URI mapping:
...
enableHeadingAttributes | false |
---|---|
enableSorting | false |
class | m5-configuration-tree |
enableHighlighting | false |
...
Node name
...
Value
...
Mgnl f |
---|
...
Mgnl f |
---|
...
Mgnl f |
---|
...
Mgnl n |
---|
...
Mgnl p |
---|
...
Mgnl p |
---|
...
Mgnl p |
---|
...
getjson
. You can call it with the following URL:
Mgnl mini code snippet |
---|
http://localhost:8080/magnoliaAuthor/getjson? |
If you want to use a RESTful URL instead:
Mgnl mini code snippet |
---|
http://localhost:8080/magnoliaAuthor/getjson/tours /magnolia-travels/Kyoto |
Add the following virtual URI mapping:
Pros
- Highly configurable.
- Fully customizable when assembling JSON manually.
- Decent customizable when using jsonfn.
- No Java required.
- Localization support.
- Delivery of personalized variants of pages or components is possible (see personalization).
...