oddmu-templates(5)

Name

oddmu-templates - how to write the templates

Synopsis

Some HTML files act as templates. They contain special placeholders in double bracers {{like this}}.

Description

Each template receives an object and uses the object's properties to replace the placeholders.

Page

A page has the following properties:

Feed

The feed contains an item for the head of the feed and an array of items.

If page A links to pages B and C, the head of the feed is based on page A and the list of items contains B and C.

An item is a page plus a date. All the properties of a page can be used (see **Page** above).

List

The list contains a directory name and an array of files.

Each file has the following attributes:

Search

A result is a page plus a score and possibly images. All the properties of a page can be used (see **Page** above).

Each image has three properties:

Upload

Non-English hyphenation

Automatic hyphenation by the browser requires two things: The style sheet must indicate "hyphen: auto" for an HTML element such as "body", and that element must have a "lang" set (usually a two letter language code such as "de" for German).

Oddmu attempts to detect the correct language for each page. It assumes that languages are not mixed on the same page. If you know that you're only going to use a small number of languages – or just a single language! – you can set the environment variable ODDMU_LANGUAGES to a comma-separated list of ISO 639-1 codes, e.g. "en" or "en,de,fr,pt".

"view.html" is used the template to render a single page and so the language detected is added to the "html" element.

"search.html" is the template used to render search results and so "en" is used for the "html" element and the language detected for every page in the search result is added to the "article" element for each snippet.

"edit.html" and "add.html" are the templates used to edit a page and at that point, the language isn't known, so "en" is used for the "html" element and no language is used for the "textarea" element.

Examples

The following link in a template takes people to today's page. If no such page exists, they are redirected to the edit form where it can be created.

<a href="/view/{{.Today}}" accesskey="t">Today</a>

The following form allows people to edit the suggested page name.

<form role="new" action="/edit/{{.Dir}}" method="GET">
  <label for="id">New page:</label>
  <input id="id" type="text" spellcheck="false" name="id"
	  accesskey="g" value="{{.Today}}" required>
  <button>Edit</button>
</form>

The following puts the current date into the text area if and only if the page itself is a blog page. Useful for *add.html*:

<textarea name="body" rows="20" cols="80" placeholder="Text" lang=""
	  autofocus required>{{- if .IsBlog}}**{{.Today}}**. {{end}}</textarea>

The following adds a list of links to parent directories. Useful for *view.html*:

<nav>
{{range .Parents}}/ <a href="{{.Url}}">{{.Title}}</a>{{end}}
</nav>

Notes

The templates are always used as-is, irrespective of the current directory. Therefore, a link to a specific page must be *absolute* or it'll point to a different page depending on the current directory.

Consider the link to "/view/index". No matter what page a visitor is looking, this takes visitors to the top "index" page. If the link points to "index" instead, it takes a visitor to the "index" page of the current directory. In this case, a visitor looking at "/view/projects/wiki" following a link to "index" ends up on "/view/projects/index", not on "/view/index".

It's up to you to decide what's best for your site, of course.

If you want a link on *upload.html* to point to the current directory's "index" page, you need to use "/view/{{.Dir}}index" because if you link to "index" the result points to "/upload/{{.Dir}}index".

Templates can be changed by uploading new copies of the template files.

Subdirectories can have their own copies of template files. One example use for this is that they can point to a different CSS file.

See also

oddmu(1)

"Structuring the web with HTML" https://developer.mozilla.org/en-US/docs/Learn/HTML

https://developer.mozilla.org/en-US/docs/Learn/HTML

"Learn to style HTML using CSS" https://developer.mozilla.org/en-US/docs/Learn/CSS

https://developer.mozilla.org/en-US/docs/Learn/CSS

The "text/template" library explains how to write templates from a programmer perspective. https://pkg.go.dev/text/template

https://pkg.go.dev/text/template

The "html/template" library explains how the templates are made more secure in a HTML context. https://pkg.go.dev/html/template

https://pkg.go.dev/html/template

"Lingua" is the library used to detect languages. https://github.com/pemistahl/lingua-go

https://github.com/pemistahl/lingua-go

Authors

Maintained by Alex Schroeder alex@gnu.org.

alex@gnu.org