-
Notifications
You must be signed in to change notification settings - Fork 235
Creating a Report Format
The included report formats include CSV, JSON, XML, HTML, SQL, Text, and a few others.
A report format consists of a single class that takes a report and renders output. When possible, the rendering stage is done using Twig templates. Binary formats or ones that aren't suitable for templates (e.g. ZIP, PDF, XLS, Email, etc.) can do the rendering within the PHP class itself.
For this tutorial, we'll create a new format that outputs a report in Markdown.
Create the file classes/local/MarkdownReportFormat.php
<?php
class MarkdownReportFormat extends ReportFormatBase {
public static function display(&$report, &$request) {
header("Content-type: text/plain");
header("Pragma: no-cache");
header("Expires: 0");
echo $report->renderReportPage('markdown/report');
}
}
This class is really simple. It handles sending the right headers and choosing the template to render.
Now, we need to create the template file. Create templates/local/markdown/report.twig
{{Name}}
=================
{% if Description %}
{{Description}}
{% endif %}
Result
-----------------
{% for row in Rows -%}
* Row {{ loop.index }}
{% for value in row.values %}
* __{{value.key}}__: {{ value.getValue() }}
{% endfor %}
{%- endfor %}
$report->options is passed into the template. This contains everything from name and description to chart/graph information to the actual report rows.
Templates use the Twig templating system. To find out more about Twig, check out their excellent documentation.
To change the format of an html report, simply replace "report/html/" in the url with "report/markdown/".
For example, if you are running the report tests/testing.php, the original url would be:
To view the same report in your new Markdown format, change it to the following: