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”
(2) Delete the root “html” node (thereby emptying the page)
(3) Open the “Edit Properties” dialog of the page and set its content-type to “text/javascript”
(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
(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:
CSV output
The basic setup is very similar to the JSON example, so only the resulting screenshot containing all the information will be shown:
- Create a new page and rename it to “pagesExport.csv”
- Delete the root “html” node
- Add a new “Template” element to the page
- Set its text to the following:
${{
// Print the Header "ID;Name;Position"
Structr.print('ID;Name;Position\n');
// Fetch all pages
var pages = Structr.find('Page');
// Iterate over all pages and print the CSV row
pages.forEach(function(page) {
Structr.print(page.id + ';' + page.name + ';' + page.position + '\n');
});
}}
The result should look like this: