Better template loading, word page

See https://stackoverflow.com/a/11468132
This commit is contained in:
Elnu 2023-07-22 16:26:40 -07:00
parent f558d7f0c1
commit e70916c6c1
11 changed files with 236 additions and 87 deletions

View file

@ -1,18 +1,25 @@
{{- define "index" -}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ with .Query }}{{ . }} search - {{ end }}jidict</title>
<title>{{ block "title" . }}{{ template "sitetitle" . }}{{ end }}</title>
<link rel="stylesheet" href="https://unpkg.com/missing.css@1.0.9/dist/missing.min.css">
<style>
li {
margin-top: 0.75em;
}
#results .box h3 a {
color: inherit;
}
#results .box h3 a:not(:hover) {
text-decoration: none;
}
</style>
<script src="https://unpkg.com/htmx.org@1.9.3"></script>
</head>
<body>
<body hx-boost="true">
<main>
<a href="/">
<img src="https://jichan.org/logo.svg" style="height: 4em; display: block; margin: 1em auto 1em auto">
@ -20,13 +27,16 @@
<form
hx-get="/search"
hx-replace-url="true"
hx-target="#results">
<input type="text" name="q"{{ with .Query }} value="{{ . }}"{{ end }} placeholder="辞書をサーチする" class="width:100%" autocomplete="false">
hx-target="#results"
hx-on::before-request="document.title = `${this.querySelector('input').value} search - jidict`">
<input type="text" name="q" value="{{ block "value" . }}{{ end }}" placeholder="辞書をサーチする" class="width:100%" autocomplete="false" required>
</form>
<div id="results">
{{ if .Count }}{{ template "search" . }}{{ end }}
{{ block "results" . }}{{ if .Query }}{{ template "search" . }}{{ end }}{{ end }}
</div>
<br>
</main>
</body>
</html>
</html>
{{- end -}}
{{- template "index" . -}}

View file

@ -0,0 +1,23 @@
{{ define "entry" }}
<div class="box">
<h3>
{{- if .Kanji -}}
<a href="/word/{{ .Kanji }}"><ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby></a>
{{- else -}}
{{- .Reading -}}
{{- end -}}
</h3>
{{- $count := len .Definitions -}}
{{ if eq $count 1 -}}
<p>{{- template "definition" (index .Definitions 0) -}}</p>
{{- else if ne $count 0 -}}
<ol>
{{- range .Definitions }}
<li>
{{ template "definition" . }}
</li>
{{- end }}
</ol>
{{- end }}
</div>
{{ end }}

View file

@ -0,0 +1,23 @@
{{ define "entryfull" }}
<div class="box">
<h3>
{{- if .Kanji -}}
<ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby>
{{- else -}}
{{- .Reading -}}
{{- end -}}
</h3>
{{- $count := len .Definitions -}}
{{ if eq $count 1 -}}
<p>{{- template "definition" (index .Definitions 0) -}}</p>
{{- else if ne $count 0 -}}
<ol>
{{- range .Definitions }}
<li>
{{ template "definition" . }}
</li>
{{- end }}
</ol>
{{- end }}
</div>
{{ end }}

View file

@ -0,0 +1,11 @@
{{- define "search" -}}
<p><i>{{ if .Truncated }}Truncated results, showing first {{ .Count }}{{ else }}{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}{{ end }}.</i></p>
{{ range .ExactResults -}}
{{- template "entry" . -}}
{{- end }}
{{ if and (ne (len .ExactResults) 0) (ne (len .OtherResults) 0) }}<hr>{{ end }}
{{ range .OtherResults -}}
{{ template "entry" . }}
{{- end -}}
{{- end -}}
{{- template "search" . -}}

View file

@ -0,0 +1 @@
{{ define "sitetitle" }}jidict{{ end }}

View file

@ -1,11 +1,9 @@
{{- define "search" -}}
<p><i>{{ if .Truncated }}Truncated results, showing first {{ .Count }}{{ else }}{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}{{ end }}.</i></p>
{{ range .ExactResults -}}
{{- template "word" . -}}
{{- end }}
{{ if and (ne (len .ExactResults) 0) (ne (len .OtherResults) 0) }}<hr>{{ end }}
{{ range .OtherResults -}}
{{ template "word" . }}
{{- define "title" }}{{ .Query }} search - {{ template "sitetitle" . }}{{- end -}}
{{- define "value" }}{{ .Query }}{{- end -}}
{{- define "results" -}}
{{- template "entryfull" .Entry -}}
{{- end -}}
{{- end -}}
{{- template "search" . -}}
{{- template "index" . -}}

View file

@ -1,22 +1,5 @@
{{ define "word" }}
<div class="box">
<h3>
{{- if .Kanji -}}
<ruby>{{- .Kanji -}}<rp>(</rp><rt>{{- .Reading -}}</rt><rp>)</rp></ruby>
{{- else -}}
{{- .Reading -}}
{{- end -}}
</h3>
{{ if le (len .Definitions) 2 -}}
<p>{{- template "definition" (index .Definitions 0) -}}</p>
{{- else -}}
<ol>
{{- range .Definitions }}
<li>
{{ template "definition" . }}
</li>
{{- end }}
</ol>
{{- end }}
</div>
{{ end }}
{{- define "title" }}{{ .Entry.Kanji }} - {{ template "sitetitle" . }}{{- end -}}
{{- define "results" -}}
{{- template "entryfull" .Entry -}}
{{- end -}}
{{- template "index" . -}}