Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: MOTION-157
HTML Wrap
floatright
classmenu

Related topics:

This page provides an overview of the different ways of retrieving JSON for JCR-based content in Magnolia as well as listing their respective advantages and disadvantages. 

...

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

...

-proyaml

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

Note that you can extend the default freemarker renderer.

Code Block
language
yml
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
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.

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

...

!inherit:freemarker
contentType: application/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.

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:

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

...

Multiexcerpt
MultiExcerptNamesame-origin-policy

According to the same-origin policy:

(...) a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin.

Wikipedia

Therefore, if your application resides within a domain that is different from the domain that hosts your data, the web browser will actively prevent accessing content stored under the host domain.

An elegant solution to solve the same-origin policy without changing Apache configuration is the Magnolia Add HTTP Headers filter. This fiter filter is available out-of-the-box; you only need to configure it.

...