Renderer::Json

Simon Courtois edited this page May 2, 2014 · 2 revisions

The Json renderer outputs a JSON string representing the full menu. This renderer can be useful when implementing a custom JavaScript menu.

The top level object is an array containing the top level menu items, every menu item has the name, url, selected and items keys. The items property contains either the array of the sub menu items or null where it is a leaf node.

Example render:

[
  {
    "name": "Books",
    "url": null,
    "selected": false,
    "items": [
      {
         "name": "Sci-Fi",
         "url" : "/sci-fi",
         "selected": true,
         "items": null
      }
    ]  
  },
  {
    "name": "Movies",
    "url": "/movies",
    "selected": false,
    "items": null
  }
]

You can invoke the renderer on the following way:

render_navigation(renderer: :json)

Most of the time you'll want the menu to be assigned directly on JavaScript, on those cases you'll problably would want to avoid the output to be escaped:

<%= raw render_navigation(renderer: :json) %>

You can also use this renderer to get the menu as a ruby Hash, to do so you need to set the option as_hash: true:

render_navigation(renderer: :json, as_hash: true)

Please be aware that even when requesting the output as a hash the results is actually an array where every item is a hash object.

You can’t perform that action at this time.
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.
Press h to open a hovercard with more details.