mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Ability to easily customize _rows_and_columns.html per database table
Also added documentation for this. Refs #158, Closes #159.
This commit is contained in:
parent
afbda9e210
commit
7e1ba161ec
4 changed files with 78 additions and 4 deletions
|
|
@ -682,6 +682,11 @@ class TableView(RowTableShared):
|
||||||
'display_columns': display_columns,
|
'display_columns': display_columns,
|
||||||
'filter_columns': filter_columns,
|
'filter_columns': filter_columns,
|
||||||
'display_rows': await self.make_display_rows(name, hash, table, rows, display_columns, pks, is_view, use_rowid, is_row_display=False),
|
'display_rows': await self.make_display_rows(name, hash, table, rows, display_columns, pks, is_view, use_rowid, is_row_display=False),
|
||||||
|
'custom_rows_and_columns_templates': [
|
||||||
|
'_rows_and_columns-{}-{}.html'.format(to_css_class(name), to_css_class(table)),
|
||||||
|
'_rows_and_columns-table-{}-{}.html'.format(to_css_class(name), to_css_class(table)),
|
||||||
|
'_rows_and_columns.html',
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -741,6 +746,11 @@ class RowView(RowTableShared):
|
||||||
'foreign_key_tables': await self.foreign_key_tables(name, table, pk_values),
|
'foreign_key_tables': await self.foreign_key_tables(name, table, pk_values),
|
||||||
'display_columns': columns,
|
'display_columns': columns,
|
||||||
'display_rows': await self.make_display_rows(name, hash, table, rows, columns, pks, is_view=False, use_rowid=use_rowid, is_row_display=True),
|
'display_rows': await self.make_display_rows(name, hash, table, rows, columns, pks, is_view=False, use_rowid=use_rowid, is_row_display=True),
|
||||||
|
'custom_rows_and_columns_templates': [
|
||||||
|
'_rows_and_columns-{}-{}.html'.format(to_css_class(name), to_css_class(table)),
|
||||||
|
'_rows_and_columns-row-{}-{}.html'.format(to_css_class(name), to_css_class(table)),
|
||||||
|
'_rows_and_columns.html',
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
<p>This data as <a href="{{ url_json }}">.json</a>, <a href="{{ url_jsono }}">.jsono</a></p>
|
<p>This data as <a href="{{ url_json }}">.json</a>, <a href="{{ url_jsono }}">.jsono</a></p>
|
||||||
|
|
||||||
{% include "_rows_and_columns.html" %}
|
{% include custom_rows_and_columns_templates %}
|
||||||
|
|
||||||
{% if foreign_key_tables %}
|
{% if foreign_key_tables %}
|
||||||
<h2>Links from other tables</h2>
|
<h2>Links from other tables</h2>
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@
|
||||||
|
|
||||||
<p>This data as <a href="{{ url_json }}">.json</a>, <a href="{{ url_jsono }}">.jsono</a></p>
|
<p>This data as <a href="{{ url_json }}">.json</a>, <a href="{{ url_jsono }}">.jsono</a></p>
|
||||||
|
|
||||||
{% include "_rows_and_columns.html" %}
|
{% include custom_rows_and_columns_templates %}
|
||||||
|
|
||||||
{% if next_url %}
|
{% if next_url %}
|
||||||
<p><a href="{{ next_url }}">Next page</a></p>
|
<p><a href="{{ next_url }}">Next page</a></p>
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,16 @@ The lookup rules Datasette uses are as follows::
|
||||||
row-mydatabase-mytable.html
|
row-mydatabase-mytable.html
|
||||||
row.html
|
row.html
|
||||||
|
|
||||||
|
Rows and columns include on table page:
|
||||||
|
_rows_and_columns-table-mydatabase-mytable.html
|
||||||
|
_rows_and_columns-mydatabase-mytable.html
|
||||||
|
_rows_and_columns.html
|
||||||
|
|
||||||
|
Rows and columns include on row page:
|
||||||
|
_rows_and_columns-row-mydatabase-mytable.html
|
||||||
|
_rows_and_columns-mydatabase-mytable.html
|
||||||
|
_rows_and_columns.html
|
||||||
|
|
||||||
If a table name has spaces or other unexpected characters in it, the template
|
If a table name has spaces or other unexpected characters in it, the template
|
||||||
filename will follow the same rules as our custom ``<body>`` CSS classes - for
|
filename will follow the same rules as our custom ``<body>`` CSS classes - for
|
||||||
example, a table called "Food Trucks" will attempt to load the following
|
example, a table called "Food Trucks" will attempt to load the following
|
||||||
|
|
@ -140,5 +150,59 @@ content you can do so by creating a ``row.html`` template like this::
|
||||||
{{ super() }}
|
{{ super() }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
Note the ``default:row.html`` template name, which ensures Jinja will inherit from the
|
Note the ``default:row.html`` template name, which ensures Jinja will inherit
|
||||||
default template.
|
from the default template.
|
||||||
|
|
||||||
|
The `_rows_and_columns.html` template is included on both the row and the table
|
||||||
|
page, and displays the content of the row. The default template looks like this::
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{% for column in display_columns %}
|
||||||
|
<th scope="col">{{ column }}</th>
|
||||||
|
{% endfor %}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for row in display_rows %}
|
||||||
|
<tr>
|
||||||
|
{% for cell in row %}
|
||||||
|
<td>{{ cell.value }}</td>
|
||||||
|
{% endfor %}
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
You can provide a custom template that applies to all of your databases and
|
||||||
|
tables, or you can provide custom templates for specific tables using the
|
||||||
|
template naming scheme described above.
|
||||||
|
|
||||||
|
Say for example you want to output a certain column as unescaped HTML. You could
|
||||||
|
provide a custom ``_rows_and_columns.html`` template like this::
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{% for column in display_columns %}
|
||||||
|
<th scope="col">{{ column }}</th>
|
||||||
|
{% endfor %}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for row in display_rows %}
|
||||||
|
<tr>
|
||||||
|
{% for cell in row %}
|
||||||
|
<td>
|
||||||
|
{% if cell.column == 'description' %}
|
||||||
|
!!{{ cell.value|safe }}
|
||||||
|
{% else %}
|
||||||
|
{{ cell.value }}
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
{% endfor %}
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue