feat: extracts {{prompt}} out from {{form}} as a separate element

This commit is contained in:
Lewis Wynne 2026-04-10 16:04:42 +01:00
parent 2e0002a810
commit 4807104eb1
5 changed files with 29 additions and 13 deletions

View file

@ -84,7 +84,7 @@
# Message textarea height in pixels. # Message textarea height in pixels.
# BOOK_TEXTAREA_HEIGHT=150 # BOOK_TEXTAREA_HEIGHT=150
# Custom HTML template file with {{title}}, {{form}}, {{entries}}, and {{style}} placeholders. # Custom HTML template file with {{title}}, {{prompt}}, {{form}}, {{entries}}, and {{style}} placeholders.
# Uses built-in default if unset. # Uses built-in default if unset.
# BOOK_TEMPLATE=./templates/default.html # BOOK_TEMPLATE=./templates/default.html

2
Cargo.lock generated
View file

@ -458,7 +458,7 @@ dependencies = [
[[package]] [[package]]
name = "guestbook" name = "guestbook"
version = "0.2.4" version = "0.2.5"
dependencies = [ dependencies = [
"axum", "axum",
"base64 0.22.1", "base64 0.22.1",

View file

@ -175,7 +175,7 @@ Running `guestbook` with no env vars will give you a working guestbook on `local
# Message textarea height in pixels. # Message textarea height in pixels.
# BOOK_TEXTAREA_HEIGHT=150 # BOOK_TEXTAREA_HEIGHT=150
# Custom HTML template file with {{title}}, {{form}}, {{entries}}, and {{style}} placeholders. # Custom HTML template file with {{title}}, {{prompt}}, {{form}}, {{entries}}, and {{style}} placeholders.
# Uses built-in default if unset. # Uses built-in default if unset.
# BOOK_TEMPLATE=./templates/default.html # BOOK_TEMPLATE=./templates/default.html
@ -344,10 +344,13 @@ The `status` field can be `pending`, `approved`, or `denied`. Only approved entr
Available placeholders: Available placeholders:
title - Site title (BOOK_SITE_TITLE). Useful in <title> and headings. title - Site title (BOOK_SITE_TITLE). Useful in <title> and headings.
prompt - The form prompt text (BOOK_FORM_PROMPT), wrapped in a
<span class="guestbook-prompt">. Empty when submissions
are disabled. Place anywhere relative to the form.
form - The submission form (labels, inputs, button). Controlled by form - The submission form (labels, inputs, button). Controlled by
BOOK_FORM_PROMPT, BOOK_LABEL_NAME, BOOK_LABEL_WEBSITE, BOOK_LABEL_NAME, BOOK_LABEL_WEBSITE, BOOK_LABEL_MESSAGE,
BOOK_LABEL_MESSAGE, BOOK_BUTTON_TEXT, BOOK_TEXTAREA_WIDTH, BOOK_BUTTON_TEXT, BOOK_TEXTAREA_WIDTH, BOOK_TEXTAREA_HEIGHT.
BOOK_TEXTAREA_HEIGHT. Empty when BOOK_ENABLE_SUBMISSIONS=false. Empty when BOOK_ENABLE_SUBMISSIONS=false.
entries - Approved guestbook entries, newest first. Entry separator entries - Approved guestbook entries, newest first. Entry separator
controlled by BOOK_SEPARATOR. controlled by BOOK_SEPARATOR.
style - Custom CSS from BOOK_STYLE or BOOK_STYLE_FILE, wrapped in style - Custom CSS from BOOK_STYLE or BOOK_STYLE_FILE, wrapped in
@ -370,6 +373,7 @@ The `status` field can be `pending`, `approved`, or `denied`. Only approved entr
guestbook guestbook
========= =========
{{prompt}}
{{form}} {{form}}
entries entries

View file

@ -13,8 +13,17 @@ pub fn render_page(template: &str, config: &Config, entries: &[Entry], form_html
&config.style &config.style
}; };
let style = format!("<style>\n{css}\n </style>"); let style = format!("<style>\n{css}\n </style>");
let prompt = if config.enable_submissions {
format!(
"<span class=\"guestbook-prompt\">{}</span>",
config.form_prompt
)
} else {
String::new()
};
template template
.replace("{{title}}", &config.site_title) .replace("{{title}}", &config.site_title)
.replace("{{prompt}}", &prompt)
.replace("{{form}}", form_html) .replace("{{form}}", form_html)
.replace("{{entries}}", &entries_html) .replace("{{entries}}", &entries_html)
.replace("{{style}}", &style) .replace("{{style}}", &style)
@ -174,8 +183,7 @@ pub fn render_form(config: &Config) -> String {
}; };
format!( format!(
r#"<span class="guestbook-prompt">{prompt}</span> r#"<form class="guestbook-form" method="post" action="/submit" accept-charset="UTF-8">
<form class="guestbook-form" method="post" action="/submit" accept-charset="UTF-8">
<label class="guestbook-label" for="name">{label_name}</label> <label class="guestbook-label" for="name">{label_name}</label>
<input class="guestbook-input" id="name" name="name" required> <input class="guestbook-input" id="name" name="name" required>
{website_section} {website_section}
@ -184,7 +192,6 @@ pub fn render_form(config: &Config) -> String {
{captcha_section} {captcha_section}
{drawing_section}{voice_note_section}<input name="url" aria-hidden="true" style="position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0)" tabindex="-1" autocomplete="off"><button class="guestbook-button" type="submit">{button}</button> {drawing_section}{voice_note_section}<input name="url" aria-hidden="true" style="position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0)" tabindex="-1" autocomplete="off"><button class="guestbook-button" type="submit">{button}</button>
</form>"#, </form>"#,
prompt = config.form_prompt,
label_name = config.label_name, label_name = config.label_name,
website_section = website_section, website_section = website_section,
label_message = config.label_message, label_message = config.label_message,
@ -437,7 +444,8 @@ mod tests {
config.button_text = "submit".into(); config.button_text = "submit".into();
config.label_name = "Name:".into(); config.label_name = "Name:".into();
let form = render_form(&config); let form = render_form(&config);
assert!(form.contains("Leave a note!")); let html = render_page(DEFAULT_TEMPLATE, &config, &[], &form);
assert!(html.contains("Leave a note!"));
assert!(form.contains("submit")); assert!(form.contains("submit"));
assert!(form.contains("Name:")); assert!(form.contains("Name:"));
} }

View file

@ -7,10 +7,13 @@
Available placeholders: Available placeholders:
title - Site title (BOOK_SITE_TITLE). Useful in <title> and headings. title - Site title (BOOK_SITE_TITLE). Useful in <title> and headings.
prompt - The form prompt text (BOOK_FORM_PROMPT), wrapped in a
<span class="guestbook-prompt">. Empty when submissions
are disabled. Place anywhere relative to the form.
form - The submission form (labels, inputs, button). Controlled by form - The submission form (labels, inputs, button). Controlled by
BOOK_FORM_PROMPT, BOOK_LABEL_NAME, BOOK_LABEL_WEBSITE, BOOK_LABEL_NAME, BOOK_LABEL_WEBSITE, BOOK_LABEL_MESSAGE,
BOOK_LABEL_MESSAGE, BOOK_BUTTON_TEXT, BOOK_TEXTAREA_WIDTH, BOOK_BUTTON_TEXT, BOOK_TEXTAREA_WIDTH, BOOK_TEXTAREA_HEIGHT.
BOOK_TEXTAREA_HEIGHT. Empty when BOOK_ENABLE_SUBMISSIONS=false. Empty when BOOK_ENABLE_SUBMISSIONS=false.
entries - Approved guestbook entries, newest first. Entry separator entries - Approved guestbook entries, newest first. Entry separator
controlled by BOOK_SEPARATOR. controlled by BOOK_SEPARATOR.
style - Custom CSS from BOOK_STYLE or BOOK_STYLE_FILE, wrapped in style - Custom CSS from BOOK_STYLE or BOOK_STYLE_FILE, wrapped in
@ -33,6 +36,7 @@
guestbook guestbook
========= =========
{{prompt}}
{{form}} {{form}}
entries entries