Creating non-HTML output


We will demonstrate how to create non-HTML output using small examples. The basic technique is identical for creating more complex output/formats. We use an empty Structr instance for clarity but you can use your own pre-existing instance.

JSON output

(1) Create a new page and rename it to “pagesExport.json”
Creating/Renaming a new Page

(2) Delete the root “html” node (thereby emptying the page)
Empty new page

(3) Open the “Edit Properties” dialog of the page and set its content-type to “text/javascript”
Page Properties Dialog

(4) Add a new “Template” element to the page
(5) Set its content type to “text/javascript”
(6) Set the text to ${to_json(find('Page'))} and save the element

When viewing the page, it should return a JSON representation of all the pages in the Structr instance. But it is a bit cluttered, so we go on:

(7) Go to the “Schema” section, edit the Page type and add a view named myExportView containing id, name and position
Page type

(8) Go back to “Pages” and edit the text you created in step 6 to ${to_json(find('Page'), 'myExportView')} (see to_json documentation for more details)

Result: Now the page and the output should look like this:
JSON output

CSV output

The basic setup is very similar to the JSON example, so only the resulting screenshot containing all the information will be shown:

  1. Create a new page and rename it to “pagesExport.csv”
  2. Delete the root “html” node
  3. Add a new “Template” element to the page
  4. Set its text to the following:

	// Print the Header "ID;Name;Position"

	// Fetch all pages
	var pages = Structr.find('Page');
	// Iterate over all pages and print the CSV row
	pages.forEach(function(page) {
		Structr.print( + ';' + + ';' + page.position + '\n');


The result should look like this:

CSV output


About this article
Last change 2017-02-28
Topics FrontendStructr 2.0