Files
my_website/views/test.html

75 lines
2.5 KiB
HTML

{{ block "test" . }}
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/htmx.org@1.9.2"></script>
<link rel="stylesheet" href="/css/index.css">
</head>
<body>
{{ template "form" .Form }}
<hr>
{{ template "display" .Data}}
</body>
<script>
document.addEventListener("DOMContentLoaded", (event) => {
document.body.addEventListener("htmx:beforeSwap", (evt) => {
console.log(evt.detail.xhr.status);
if (evt.detail.xhr.status === 422) {
evt.detail.shouldSwap = true;
evt.detail.isError = false;
}
});
});
</script>
</html>
{{ end }}
{{ block "form" . }}
<form hx-swap="outerHTML" hx-post="/contacts">
name: <input
{{ if .Values.name }} value="{{ .Values.name }}" {{ end }}
type="text" name="name">
email: <input
{{ if .Values.email }} value="{{ .Values.email }}" {{ end }}
type="text" name="email">
{{ if .Errors.email }}
<div style="color:red;">{{ .Errors.email }}</div>
{{ end }}
<button type="submit">Create Contact</button>
</form>
{{ end }}
{{ block "display" .}}
<div id="contacts" style="display:flex; flex-direction: column;">
{{ range .Contacts }}
{{ template "contact" . }}
{{ end }}
</div>
{{ end }}
{{ block "contact" .}}
<div class="contact" id="contact-{{ .Id }}" style="display: flex;">
<div hx-indicator="#ci-{{ .Id }}" hx-target="#contact-{{ .Id }}" hx-swap="outerHTML swap:500ms" hx-delete="/contacts/{{ .Id }}" style="width: 1rem;">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="none" d="M0 0h24v24H0z"/>
<path d="M4 2h16a1 1 0 011 1v1a1 1 0 01-1 1H4a1 1 0 01-1-1V3a1 1 0 011-1zM3 6h18v16a1 1 0 01-1 1H4a1 1 0 01-1-1V6zm3 3v9a1 1 0 002 0v-9a1 1 0 00-2 0zm5 0v9a1 1 0 002 0v-9a1 1 0 00-2 0zm5 0v9a1 1 0 002 0v-9a1 1 0 00-2 0z"/>
</svg>
</div>
Name: <span>{{ .Name }}</span>
Email: <span>{{ .Email }}</span>
<div id="ci-{{ .Id }}" class="htmx-indicator">
<img src="/images/bars.svg" alt="loading" style="width: 1rem">
</div>
</div>
{{ end }}
{{ block "oob-contact" .}}
<div hx-swap-oob="afterbegin" id="contacts">
{{ template "contact" . }}
</div>
{{ end }}