Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: MOTION-157

...

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

Note that you can extend the default freemarker renderer.

Localtab Group
Localtab
activetrue
titleYAML file
Code Block
languageyml
title/module-a/renderers/json.yaml
urlhttps://git.magnolia-cms.com/projects/DOCUMENTATION/repos/having-fun-with-yaml/raw/light-modules/module-a/renderers/json.yaml?at=master
!inherit:freemarker
contentType: application/json

The above example requires the Magnolia specific !inherit directive available since Magnolia 5.5.6

Localtab
titleJCR node
Advanced Tables - Table Plus
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse

Node name

Value

Mgnl f
modules

Mgnl f
rendering

Mgnl f
renderers

Mgnl n
json

Mgnl p
contentType

application/json

Mgnl p
extends

../freemarker

Mgnl p
type

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? workspace=tours & path=/magnolia-travels/Kyoto

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:

...

enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse

...

Node name

...

Value

...

Mgnl f
modules

...

Mgnl f
<your-module>

...

Mgnl f
virtualURIMapping

...

Mgnl n
getjson-fromWorkspace-byPath

...

Mgnl p
class

...

Mgnl p
fromURI

...

Mgnl p
toURI

...

getjson. You can call it with the following URL: 

Mgnl mini code snippet

http://localhost:8080/magnoliaAuthor/getjson? workspace=tours & path=/magnolia-travels/Kyoto

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:

Image Added

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).

...