Skip to content

Commit b797cda

Browse files
committed
allow customization of empty lists
1 parent 3223929 commit b797cda

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Better error messages. SQLPage displays a more precise and useful message when an error occurs, and displays the position in the SQL statement where the error occured. Incorrect error messages on invalid migrations are also fixed.
3636
- We now distribute docker images from ARM too. Say hello to SQLPage on your Raspberry Pi and your Mac M1 !
3737
- Create the default SQLite database file in the "sqlpage" config directory instead of at the root of the web server by default. This makes it inaccessible from the web, which is a more secure default. If you want to keep the old behavior, set the `database_url` configuration parameter to `sqlite://sqlpage.db` in your [configuration](./configuration.md).
38+
- New `empty_title`, `empty_description`, and `empty_link` top-level attributes on the [`list`](https://sql.ophir.dev/documentation.sql?component=list#component) component to customize the text displayed when the list is empty.
3839

3940
## 0.11.0 (2023-09-17)
4041
- Support for **environment variables** ! You can now read environment variables from sql code using `sqlpage.environment_variable('VAR_NAME')`.

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ INSERT INTO component(name, icon, description) VALUES
2727
INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'list', * FROM (VALUES
2828
-- top level
2929
('title', 'Text header at the top of the list.', 'TEXT', TRUE, TRUE),
30+
('empty_title', 'Title text to display if the list is empty.', 'TEXT', TRUE, TRUE),
31+
('empty_description', 'Description to display if the list is empty.', 'TEXT', TRUE, TRUE),
32+
('empty_description_md', 'Description to display if the list is empty, in Markdown format.', 'TEXT', TRUE, TRUE),
33+
('empty_link', 'URL to which the user should be taken if they click on the empty list.', 'URL', TRUE, TRUE),
3034
-- item level
3135
('title', 'Name of the list item, displayed prominently.', 'TEXT', FALSE, FALSE),
3236
('description', 'A description of the list item, displayed as greyed-out text.', 'TEXT', FALSE, TRUE),
@@ -39,6 +43,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
3943

4044
INSERT INTO example(component, description, properties) VALUES
4145
('list', 'The most basic list', json('[{"component":"list"},{"title":"A"},{"title":"B"},{"title":"C"}]')),
46+
('list', 'An empty list with a link to add an item', json('[{"component":"list", "empty_title": "No items yet", "empty_description": "This list is empty. Click here to create a new item !", "empty_link": "documentation.sql"}]')),
4247
('list', 'A list with rich text descriptions', json('[{"component":"list"},
4348
{"title":"SQLPage", "description_md":"A **SQL**-based **page** generator for **PostgreSQL**, **MySQL**, and **SQLite**. [Free on Github](https://github.com/lovasoa/sqlpage)"},
4449
{"title":"Tabler", "description_md":"A **free** and **open-source** **HTML** template pack based on **Bootstrap**."},

sqlpage/templates/list.handlebars

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,20 @@
2929
</div>
3030
</{{#if link}}a {{else}}div{{/if}}>
3131
{{/each_row}}
32+
{{#if (eq @row_index 0)}}
33+
<a href="{{default empty_link '#'}}" class="list-group-item list-group-item-action">
34+
<div class="row align-items-center">
35+
<div class="col text-truncate">
36+
{{default empty_title 'No item'}}
37+
<div class="d-block text-muted text-truncate mt-n1">
38+
{{~empty_description~}}
39+
{{~#if empty_description_md~}}
40+
<div style="margin-bottom: -1rem">{{{markdown empty_description_md}}}</div>
41+
{{~/if~}}
42+
</div>
43+
</div>
44+
</div>
45+
</a>
46+
{{/if}}
3247
</div>
3348
</div>

src/render.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,15 @@ impl SplitTemplateRenderer {
597597
"Closing a template {}",
598598
self.split_template
599599
.name()
600-
.map(|n| format!("('{n}')"))
600+
.map(|name| format!("('{name}')"))
601601
.unwrap_or_default(),
602602
);
603-
if let Some(local_vars) = self.local_vars.take() {
603+
if let Some(mut local_vars) = self.local_vars.take() {
604604
let mut render_context = handlebars::RenderContext::new(None);
605+
local_vars.put("row_index", self.row_index.into());
606+
log::info!(
607+
"Rendering the after_list template with the following local variables: {local_vars:?}"
608+
);
605609
*render_context
606610
.block_mut()
607611
.expect("ctx created without block")

0 commit comments

Comments
 (0)