### Defining Content Block Start - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artform.html Marks the beginning of the 'content' block, where the main page content will be rendered. This block is typically filled by other templates or direct HTML. ```Go Template {{define "content"}} ``` -------------------------------- ### Iterating Over Collections in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/load.html These examples show how to use the `range` action in Go templates to iterate over slices or arrays. The `if` action is nested to conditionally render content based on file properties like `IsDir` or custom functions like `isLegendsXml`. ```Go Template {{- range .Partitions }} {{- end }} {{- range $f := .List }}{{- if $f.IsDir }} {{- end }}{{- end }} {{- range $f := .List }}{{- if isLegendsXml $f }} {{- end }}{{- end }} ``` -------------------------------- ### Rendering Entity Leaders List in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/entity.html This snippet iterates through a list of leaders associated with the entity. For each leader, it displays their tenure (start and end year) and links to their historical figure detail page. ```Go Template {{ $active := " active"}} {{- if gt (len .Leaders) 0 }} {{- range .Leaders }} {{- end}} Date Name {{- if eq .EndYear -1 }} since {{ .StartYear }} {{- else }} from {{ .StartYear }} till {{ .EndYear }} {{- end }} {{ hf .Hf.Id }} {{ $active = ""}}{{- end}} ``` -------------------------------- ### Displaying Event Details and Including Nested Template (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/collectionDetail.html Within the range loop, this snippet uses `with getCollection .` to access event details. It then displays the event's start year and seconds, its ID, and includes a nested template `events.html` to render additional event-specific information, passing `events` and `.Event` data. ```Go Template * {{ with getCollection . }} In {{ time .StartYear .StartSeconds72 }}, {{ collection .Id }} occurred {{ template "events.html" events .Event }} {{ end }} ``` -------------------------------- ### Initializing Autocomplete with jQuery and Custom Data Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/layout.html This snippet initializes an Autocomplete component on a search input field. It fetches data dynamically via a jQuery GET request on input and redirects the window location upon item selection. The 'maximumItems' option limits the number of displayed suggestions. ```JavaScript const ac = new Autocomplete(document.getElementById("search"), { data: [{ label: "I'm a label", value: 42 }], maximumItems: 50, onInput: value => $.get("./search?term=" + value, data => ac.setData(data)), onSelectItem: ({ label, value }) => window.location = "." + value }); ``` -------------------------------- ### Starting Outer Data Range Loop - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artifacts.html This snippet initiates an outer range loop, iterating over the main data context. The loop variable `$t` typically represents a type or category, and `$v` represents the collection of items for that type, enabling structured data processing. ```Go Template {{- range $t, $v := . }} ``` -------------------------------- ### Leaflet Map Initialization and Feature Loading (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/worldMap.html This Go template block conditionally initializes a Leaflet map if `world.MapReady` is true. It includes a call to `initMap` (expected to generate Leaflet map setup JavaScript) and then adds a Leaflet layer control. Subsequent `range` loops iterate over various geographical entities (landmasses, regions, sites, mountain peaks, world constructions, and rivers), calling Go template functions like `addLandmass` and `addRiver`. These functions are designed to generate JavaScript code that adds the respective features to the Leaflet map, enabling dynamic visualization of world data. ```Go Template {{ if world.MapReady }} {{initMap}} L.control.layers(null, overlayMaps).addTo(map); {{- range .Landmasses }} {{ addLandmass .Id }} {{- end }} {{- range .Regions }} {{ addRegion .Id }} {{- end }} {{- range .Sites }} {{ addSite .Id true }} {{- end }} {{- range .MountainPeaks }} {{ addMountain .Id true }} {{- end }} {{- range .WorldConstructions }} {{ addWorldConstruction .Id }} {{- end }} {{- range $id, $r := .Rivers }} {{ addRiver $id }} {{- end }} {{ else }} No map data available {{- end }} ``` -------------------------------- ### Handling Bootstrap Tab Navigation with URL Hash in JavaScript Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/layout.html This JavaScript code manages Bootstrap tab visibility based on the URL hash. It automatically activates the corresponding tab if the hash starts with '#nav-', and updates the URL hash when a tab button or link is clicked, ensuring persistent navigation state. ```JavaScript var hash = document.location.hash; if (hash && hash.startsWith("#nav-")) { var hashPieces = hash.split('?'); var someTabTriggerEl = document.querySelector('.nav-link[data-bs-target="' + hashPieces[0] + '"]') var tab = new bootstrap.Tab(someTabTriggerEl) tab.show() } $('.nav-tabs button,.nav-tabs a').on('click', function (e) { window.location.hash = $(this).data("bs-target") }); ``` -------------------------------- ### Initializing Map and Plotting Sites in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/entity.html This snippet conditionally initializes a map and plots the entity's associated sites if the `world.MapReady` flag is true and there are sites to display. It calls `initMap` and `addSite` functions. ```Go Template {{ if world.MapReady }} {{- if gt (len .Sites) 0 }} {{initMap}} {{- range .Sites }} {{ addSite . false }} {{- end }} {{- end }} {{- end }} ``` -------------------------------- ### Constructing Dynamic URLs in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/load.html These snippets illustrate how to build dynamic hyperlinks using Go template variables and functions like `printf`. They are used to create navigation links for files and directories, including a 'go up' link and conditional rendering based on custom functions like `isLegendsXmlWithPlus`. ```Go Template [{{ . }}](./load?p={{ . }}) [..](./load?p={{ \(printf `%s/..` $.Current\) }}&x=1) [{{$f.Name}}](./load?p={{ \(printf `%s/%s` $.Current $f.Name\) }}) [{{$f.Name}} {{- if isLegendsXmlWithPlus $.Current $f }} {{- end }}](./load?p={{ \(printf `%s/%s` $.Current $f.Name\) }}) ``` -------------------------------- ### Displaying Basic Site Information in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/site.html This snippet renders the site's name as a main heading and displays its type. It utilizes Go template functions to dynamically insert these values into the HTML output, providing initial context for the site details. ```Go Template {{ title .Name }} {{ .Type_ }} ``` -------------------------------- ### Command Line Options for Legends Browser 2 (Shell) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/README.md This snippet lists the available command-line options for running Legends Browser 2. These options allow users to specify the port, enable server mode, define a sub-URI, or provide a direct path to the legends.xml file or archive. ```Shell -p,--port use specific port -s,--serverMode run in server mode (disables file chooser) -u,--subUri run on / -w,--world path to legends.xml or archive ``` -------------------------------- ### Accessing Context Field in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverRiver.html This Go template snippet illustrates how to access the 'Id' field of the current data context (represented by '.') and pass its value as an argument to a 'river' function or pipeline. This pattern is commonly used to display or process specific data attributes within a template. ```Go Template {{ river .Id }} ``` -------------------------------- ### Defining Page Layout and Content Blocks in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/sites.html This snippet sets up the basic structure of the HTML page using Go templates. It includes the 'layout.html' template, defines the page title as 'Sites', and marks the main content area for rendering. ```Go Template {{template "layout.html" .}} {{define "title"}}Sites{{end}} {{define "content"}} ``` -------------------------------- ### Defining and Including Go Templates Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/load.html These snippets demonstrate how to include a base layout template and define specific content blocks (like title and main content) within a Go HTML template. This modular approach helps in structuring web pages. ```Go Template {{template "layout.html" .}} {{define "title"}}Loading{{end}} {{define "content"}} ``` -------------------------------- ### Rendering Current and Former Entity Positions in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/hf.html This code block displays both current and former positions held by the legend within various entities. It iterates through `EntityFormerPositionLink` and `EntityPositionLink` slices, using `getEntity` and `entity` functions to resolve entity and position details, including start and end years. ```Go Template {{- if or (ne 0 (len .EntityFormerPositionLink)) (ne 0 (len .EntityPositionLink)) }} ##### Positions {{- range $i := .EntityFormerPositionLink }}* {{ ((getEntity $i.EntityId).Position $i.PositionProfileId).GenderName $ }} of {{ entity $i.EntityId }} ({{ $i.StartYear }} - {{ $i.EndYear }}) {{- end }} {{- range $i := .EntityPositionLink }}* {{ ((getEntity $i.EntityId).Position $i.PositionProfileId).GenderName $ }} of {{ entity $i.EntityId }} (since {{ $i.StartYear }}) {{- end }} ``` -------------------------------- ### Go Template Layout and Title Definition Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/structures.html This snippet sets up the base HTML layout by including 'layout.html', defines the page title as 'Structures', and initiates the main content block for the page. It's crucial for establishing the overall page structure. ```Go Template {{template "layout.html" .}} {{define "title"}}Structures{{end}} {{define "content"}} ``` -------------------------------- ### Rendering Page Description - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artform.html Renders a description for the page, using the 'Description' field from the context object, processed by a 'description' function for display. ```Go Template {{ description .Description }} ``` -------------------------------- ### Including Base Layout - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artform.html Includes the 'layout.html' template, passing the current context object '.' to it. This sets up the overall page structure for the rendered HTML. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Defining HTML Layout and Content Blocks in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/landmass.html This snippet sets up the basic structure of an HTML page by including a `layout.html` template, defines a `title` block to dynamically set the page's title, and initiates a `content` block where the main page content will reside. ```Go Template {{template "layout.html" .}} {{define "title"}}{{ title .Name }}{{end}} {{define "content"}} ``` -------------------------------- ### Rendering Page Heading - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artform.html Renders the main heading for the page, using the 'Name' field from the context object, processed by a 'title' function for formatting and display. ```Go Template {{ title .Name }} ``` -------------------------------- ### Formatting Data for Display in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/load.html These snippets demonstrate how to apply custom functions (`bytes`, `json`) and standard formatting (`Format`) to data within a Go template. This is used to present file sizes in a human-readable format, display modification times, and output data as JSON. ```Go Template {{ bytes $f.Info.Size }} {{ $f.Info.ModTime.Format "02 Jan 06 15:04" }} {{ json . }} ``` -------------------------------- ### Initializing Map and Adding Site Marker in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/site.html This snippet conditionally initializes a map and adds the current site's location to it, but only if the 'world.MapReady' function returns true. This ensures map-related functions are called only when the map system is prepared and available. ```Go Template {{ if world.MapReady }} {{initMap}} {{ addSite .Id false }} {{- end }} ``` -------------------------------- ### Conditionally Rendering Map and Adding Layer Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/landmass.html This block checks if the `world.MapReady` condition is true. If so, it initializes a map using `initMap`, adds a landmass with `addLandmass .Id`, and then executes a JavaScript line to add `landmassesLayer` to the map. This demonstrates conditional content rendering and integration of client-side JavaScript within the template. ```Go Template {{ if world.MapReady }} {{initMap}} {{ addLandmass .Id }} ``` ```JavaScript map.addLayer(landmassesLayer); ``` ```Go Template {{- end }} ``` -------------------------------- ### Displaying Site and Entity Details - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverSite.html This Go template snippet renders various attributes of a 'site' object. It displays the site's ID, type, and conditionally appends '(ruin)' if the 'Ruin' property is true. It also conditionally displays the owner's name if the 'Owner' property is not -1, using an 'entity' helper function. ```Go Template {{ site .Id }} {{ .Type }}{{if .Ruin}} (ruin){{end}}{{ if ne .Owner -1}} of {{ entity .Owner }}{{end}} ``` -------------------------------- ### Defining Content Block and Initializing Map in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/structure.html This block defines the main content area of the page. It conditionally initializes a map and adds a specific site (`.SiteId`) to it only if the `world.MapReady` flag is true, ensuring map-related operations occur only when the map is prepared. ```Go Template {{define "content"}} {{ if world.MapReady }} {{initMap}} {{ addSite .SiteId false }} {{- end }} ``` -------------------------------- ### Including Layout Template (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/mountain.html This snippet includes the `layout.html` template, passing the current context (`.`) to it. This is a common pattern for defining a consistent page structure and applying a global layout. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Displaying Artifact Details in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverArtifact.html This Go template snippet renders various properties of an artifact. It conditionally displays the material, item type/subtype, written content, storage site, and holder based on whether these properties are present or have specific values (e.g., not empty strings or -1). The `{{- ... -}}` syntax is used for whitespace trimming, ensuring clean output. ```Go Template {{ artifact .Id }} {{- if or (ne .ItemType "") (ne .ItemSubtype "")}} {{.Mat}} {{if ne .ItemSubtype ""}}{{.ItemSubtype}}{{else}}{{.ItemType}}{{end}} {{- end}} {{- if ne .Writing -1 }} containing {{ writtenContent .Writing}} {{- end}} {{- if ne .SiteId -1 }} stored in {{ site .SiteId}} {{- end}} {{- if ne .HolderHfid -1 }} owned by {{ hf .HolderHfid}} {{- end}} ``` -------------------------------- ### Defining a 'Not Found' Page with Go HTML Templates Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/notFound.html This snippet demonstrates how to construct a simple 'Not Found' page using Go's `html/template` package. It first includes a shared 'layout.html' template, then defines a 'title' block with 'Not Found', and finally a 'content' block containing the 'Not found' message. The `{{- end }}` syntax is used to trim trailing whitespace. ```Go Template {{template \"layout.html\" .}} {{define \"title\"}}Not Found{{end}} {{define \"content\"}} Not found {{- end }} ``` -------------------------------- ### Displaying Page Name, Type, and Site in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/structure.html This snippet renders the page's name as a heading, followed by its type and the associated site ID. It uses template functions `title` and `site` to format and retrieve the respective data from the context. ```Go Template ### {{ title .Name }} {{ .Type_ }} in {{ site .SiteId }} ``` -------------------------------- ### Including Events Template - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artform.html Includes the 'events.html' template, passing both an 'events' variable and the current context object '.' to it for rendering event-related content dynamically. ```Go Template {{ template "events.html" events . }} ``` -------------------------------- ### Conditional Map Rendering and Site Initialization in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/index.html This Go template block conditionally renders map-related content if 'world.MapReady' is true. It displays the world's name and alternative name if available, initializes the map using 'initMap', and iterates through civilizations and their sites to add them to the map. ```Go Template {{ if world.MapReady }} {{- if ne world.Name "" -}} ### {{world.Name}} - {{world.Altname}} {{- end }} {{initMap}} {{- range $race, $civs := .Civilizations }}{{- range $civs }}{{- range .Sites }} {{ addSite . true }} {{- end }}{{- end }}{{- end }} {{- end }} ``` -------------------------------- ### Rendering a Detail Page with Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/writtencontent.html This Go HTML template defines the layout and content for a detail page. It utilizes `layout.html` for the overall structure, sets the page title, and dynamically renders various fields such as the entry's name, form, author, references, and style. It also includes an `events.html` template and outputs the entire context as JSON for debugging or API purposes. ```Go Template {{template "layout.html" .}} {{define "title"}}{{ title .Name }}{{end}} {{define "content"}} ### {{ title .Name }} {{ .Form }} by {{ hf .AuthorHfid }} {{- if ne .FormId -1 }} an example of {{- if eq .Form.String "musical composition" }} {{ musicalForm .FormId }} {{- else if eq .Form.String "choreography" }} {{ danceForm .FormId }} {{- else }} {{ poeticForm .FormId }} {{- end }} {{- end }} {{- if ne 0 (len .Reference) }} ##### References {{- range .Reference }}* {{ .Html (context $) }} {{- end }} {{- end }} {{- if ne 0 (len .Style) }} ##### Style {{- range .Style }}* {{ . }} {{- end }} {{- end }} ##### Events {{ template "events.html" events . }} {{ json . }} {{- end }} ``` -------------------------------- ### Iterating and Displaying Collection Length in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/sites.html This Go template snippet iterates over a collection (likely a map or slice) and prints each key/type along with the count of its associated values using the `len` function. The `-` syntax is used to trim whitespace. ```Go Template {{- range $t, $v := .}} {{$t}} ({{ len $v }}) {{- end}} ``` -------------------------------- ### Iterating and Displaying Events in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/events.html This snippet ranges over a slice of `Events`, formatting each event's year and seconds into a human-readable time. It then renders event details as HTML, conditionally adds a link to a collection based on the `Collection` field, and finally outputs the event details as JSON. It requires `Events` to be available in the context, along with `time`, `html`, and `json` template functions. ```Go Template {{- range $event := .Events }}* In {{ time $event.Year $event.Seconds72 }}, {{ html ($event.Details.Html ($.Context.WithEvent $event)) }} {{ if ne .Collection -1 }} [](./collection/{{.Collection}}){{end}} {{ json $event.Details }} {{- end}} ``` -------------------------------- ### Including Events Template (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/mountain.html This snippet includes another template named `events.html`, passing the `events` data and the current context (`.`) to it. This modularizes the display of event information, keeping the main template cleaner. ```Go Template ##### Events {{ template "events.html" events . }} ``` -------------------------------- ### Displaying World Construction Details with Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/worldconstruction.html This Go HTML template renders a detailed view of a 'world construction' or 'legend' entry. It integrates with a base layout, dynamically displays the entity's name and type, lists its constituent parts, and embeds a separate events template. It also includes conditional map initialization and outputs the full data object as JSON. ```Go Template {{template "layout.html" .}} {{define "title"}}{{ title .Name }}{{end}} {{define "content"}} {{ if world.MapReady }} {{initMap}} {{ addWorldConstruction .Id }} {{- range .Parts }} {{ addWorldConstruction . }} {{- end }} {{- end }} ### {{ title .Name }} {{ .Type }} {{- if gt (len .Parts) 0 }} ##### Parts {{- range .Parts }}* {{ worldConstruction . }} {{- end }} {{- end }} ##### Events {{ template "events.html" events . }} {{ json . }} {{- end }} ``` -------------------------------- ### Including Base Layout in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/search.html This snippet includes a base HTML layout template, 'layout.html', passing the current context ('.') to it. This ensures consistent page structure and styling across different views. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Rendering Entity Sites List and Events in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/entity.html This snippet iterates through a list of sites associated with the entity. For each site, it displays its name and includes a template for rendering its historical events. ```Go Template {{- if gt (len .Sites) 0 }} {{- range .Sites }} {{- end}} Name History {{ site . }} {{ template "events.html" events (history .) }} {{ $active = ""}}{{- end}} ``` -------------------------------- ### Including Events Partial in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/structure.html This snippet includes another Go template file, `events.html`, to render a list of events. It passes the `events` data and the current context (`.`) to the partial template for rendering. ```Go Template ##### Events {{ template "events.html" events . }} ``` -------------------------------- ### Including Base Layout in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/serverMode.html This snippet includes a base HTML layout file, 'layout.html', into the current template. The dot '.' represents the data passed to the template, making it available within 'layout.html'. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Defining Identities Page Structure with Go Templates Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/identities.html This snippet illustrates the complete Go template structure for rendering an 'Identities' page. It includes the `layout.html` template, defines the `title` and `content` blocks, and demonstrates iterating over a data slice (`.`) to display identity details. It also shows conditional rendering and calls to custom helper functions `identity` and `hf`. ```Go Template {{template "layout.html" .}} {{define "title"}}Identities{{end}} {{define "content"}} ### Identities {{- range . }}{{- if not (eq .Name "") }} {{- end}}{{- end}} Name Used By {{ identity .Id }} {{ hf .HistfigId }} {{- end }} ``` -------------------------------- ### Including Layout and Defining Page Title (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/geography.html This Go template snippet initiates the page rendering by including the `layout.html` template, passing the current context (`.`). It then defines the title for the current page as 'Geography', which will be used by the `layout.html` template. ```Go Template {{template "layout.html" .}} {{define "title"}}Geography{{end}} {{define "content"}} ``` -------------------------------- ### Defining Layout and Page Title in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/site.html This snippet defines the overall layout for the page using 'layout.html' and sets the page title dynamically based on the site's name. It also marks the beginning of the main content block where the rest of the page's content will be rendered. ```Go Template {{template "layout.html" .}} {{define "title"}}{{ title .Name }}{{end}} {{define "content"}} ``` -------------------------------- ### Including Layout Template - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artifacts.html This snippet includes the 'layout.html' template, passing the current context ('.') to it. It serves as the base structure for the page, allowing for consistent page design. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Rendering Musical Form Details - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artforms.html This Go template snippet processes the 'MusicalForms' collection. It includes placeholder loops and then outputs static 'Name' and 'Size' headers, followed by a call to the 'musicalForm' function, passing the 'Id' of the current musical form. The 'active' variable is reset after the range, likely for UI state management. ```Go Template {{ $active := " active"}} {{- range $t, $v := .MusicalForms }} {{- range $v }}{{- if not (eq .Name "") }} {{- end}}{{- end}} Name Size {{ musicalForm .Id }} {{ $active = ""}}{{- end}} ``` -------------------------------- ### Displaying Site Details with Nested Iteration in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/sites.html This complex Go template block iterates through nested site data. It includes an inner loop that checks if a site's name is not empty, then displays the site's ID (via `site .Id`), the count of its structures (`len .Structures`), and the count of its properties (`len .SiteProperties`). ```Go Template {{- range $t, $v := . }} {{- range $v }}{{- if not (eq .Name "") }} {{- end}}{{- end}} {{ site .Id }} {{ len .Structures }} {{ len .SiteProperties }} {{- end}} {{- end }} ``` -------------------------------- ### Go Template for Structures Page Content Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/structures.html This comprehensive Go template snippet renders the main content for the 'Structures' page. It includes iteration over top-level data to display summaries, performs nested loops with conditional rendering, and calls custom template functions ('structure', 'site') to display detailed information for each item based on its SiteId and Id. ```Go Template ### Structures {{- range $t, $v := .}} {{$t}} ({{ len $v }}) {{- end}} {{- range $t, $v := . }} {{- range $v }}{{- if not (eq .Name "") }} {{- end}}{{- end}} Name Site {{ structure .SiteId .Id }} {{ site .SiteId }} {{- end}} {{- end }} ``` -------------------------------- ### Including Base Layout - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/identity.html This snippet includes the base 'layout.html' template, passing the current context ('.') to it. This establishes the fundamental HTML structure and common elements for the page. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Defining Page Layout and Title in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/entities.html This snippet sets up the basic layout for the HTML page by including `layout.html` and defines the page's title as "Entities". It's a standard practice for structuring web pages with reusable components. ```Go Template {{template "layout.html" .}} {{define "title"}}Entities{{end}} {{define "content"}} ``` -------------------------------- ### Iterating and Displaying Map/Slice Length in Go HTML Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/worldconstructions.html This snippet iterates over a map or slice (`.`) where `$t` is the key/index and `$v` is the value. It prints the key/index followed by the count of items in the value (`len $v`), enclosed in parentheses. ```Go Template {{- range $t, $v := .}} {{$t}} ({{ len $v }}) {{- end}} ``` -------------------------------- ### Defining Layout and Page Title in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/index.html This snippet sets up the basic structure of an HTML page using Go templates. It includes a 'layout.html' template, defines the page title as 'Legends Browser', and marks the content block for dynamic insertion. ```Go Template {{template "layout.html" .}} {{define "title"}}Legends Browser{{end}} {{define "content"}} ``` -------------------------------- ### Displaying Current Path in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/load.html This snippet accesses and displays the `Current` field from the template's data context. It's typically used to show the user's current location or directory in a file browser interface. ```Go Template {{ .Current }} ``` -------------------------------- ### Generating Entity Description in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverEntity.html This Go template snippet constructs a detailed description for an entity. It conditionally appends information such as race, necromancer status, type, profession, parent entity, worship details (for religions and military units), and weapon mastery (for military units). It utilizes template functions like `entity`, `hfList`, and `andList` for data retrieval and formatting, ensuring a concise and context-aware output. ```Go Template {{ entity .Id }} {{ .Race }}{{ if .Necromancer}} necromancer{{end}} {{ .Type }} {{- if .Profession }} of {{ .Profession }}s {{- end }} {{- if ne .Parent -1 }} of {{ entity .Parent }} {{- end }} {{- if and (eq .Type "religion") (gt (len .WorshipId) 0) }} centered around the worship of {{ hfList .WorshipId }} {{- end }} {{- if eq .Type "militaryunit" }} {{- if gt (len .WorshipId) 0 }} devoted to the worship of {{ hfList .WorshipId }} {{- end }} {{- if gt (len .WorshipId) 0 }} dedicated to the mastery of {{ andList .Weapons }} {{- end }} {{- end }} ``` -------------------------------- ### Including Layout Template in Go HTML Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/worldconstructions.html This snippet demonstrates how to include a base layout template, `layout.html`, into the current template. The `.` represents the current data passed to the template, making it available to the included layout. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Defining Page Layout and Title in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/structure.html This snippet sets the base HTML layout for the page using `layout.html` and defines the page's title dynamically based on the `.Name` property of the current context. ```Go Template {{template "layout.html" .}} {{define "title"}}{{ title .Name }}{{end}} ``` -------------------------------- ### Defining Main Layout in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/eventType.html This snippet includes a base layout template, passing the current context. It establishes the foundational HTML structure for the page, ensuring consistency across different views. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Defining Main Content Block - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/year.html This snippet defines the main 'content' block of the page. It includes a dynamic heading for the year, a static 'Events' heading, and embeds the `events.html` template, passing both the `events` data and the current context for rendering. ```Go Template {{define "content"}} ### Year {{(index . 0).Year}} ##### Events {{ template "events.html" events . }} {{- end }} ``` -------------------------------- ### Displaying Site Element Details in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverStructure.html This Go template snippet renders details about a specific site element. It uses `structure .SiteId .Id` to display the hierarchical structure of the element, followed by its `.Type`. An `if` condition checks if `.Ruin` is true to append '(ruin)'. Finally, it displays the name of the parent site using `site .SiteId`. ```Go Template {{ structure .SiteId .Id }} {{ .Type }}{{if .Ruin}} (ruin){{end}} in {{ site .SiteId }} ``` -------------------------------- ### Defining and Including Layout in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/hfs.html This snippet demonstrates how to include a base layout template (`layout.html`) and define specific content blocks (`title`, `content`) within a Go HTML template. The `.` represents the current data context passed to the template. The final `{{- end }}` closes the `content` block. ```Go Template {{template "layout.html" .}} {{define "title"}}Historical Figures{{end}} {{define "content"}} {{- end }} ``` -------------------------------- ### Conditional Map Initialization and Mountain Addition (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/mountain.html This block conditionally initializes a map and adds a mountain marker if the `world.MapReady` variable is true. It calls `initMap` and `addMountain` functions, passing the mountain's ID and a boolean flag for rendering. ```Go Template {{ if world.MapReady }} {{initMap}} {{ addMountain .Id false }} {{- end }} ``` -------------------------------- ### Defining Layout and Title in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/entity.html This snippet defines the overall layout and sets the page title using Go template actions. It includes a base layout template and dynamically generates the title from the entity's name. ```Go Template {{template "layout.html" .}} {{define "title"}}{{ title .Name }}{{end}} {{define "content"}} ``` -------------------------------- ### Defining Page Title Block - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artform.html Defines the 'title' block within the template, setting the HTML page title to the 'Name' field of the current context object, processed by a 'title' function for proper formatting. ```Go Template {{define "title"}}{{ title .Name }}{{end}} ``` -------------------------------- ### Rendering World Construction and Type in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverWorldconstruction.html This Go template snippet demonstrates how to invoke a template function `worldConstruction` with a data field `.Id` as an argument, and how to display another data field `.Type`. It's typically used within a larger HTML or text template to dynamically generate content based on the current data context. ```Go Template {{ worldConstruction .Id }} {{ .Type }} ``` -------------------------------- ### Displaying All Events and JSON Data and Closing Content Block in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/site.html This final section renders all events associated with the current site by including the 'events.html' template. Additionally, it outputs the entire site data object as a JSON string, likely for debugging or client-side consumption, and closes the main content definition block. ```Go Template {{ template "events.html" events . }} {{ json . }} {{- end }} ``` -------------------------------- ### Rendering Context as JSON - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artform.html Renders the entire current context object '.' as a JSON string. This is often used for debugging purposes or embedding data directly into the HTML for client-side scripts. ```Go Template {{ json . }} ``` -------------------------------- ### Integrating Layout and Defining Content Blocks in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/regions.html This snippet integrates the `layout.html` template, passes the current context, and defines the 'title' and 'content' blocks for the page. The 'title' is set to 'Regions', and the 'content' block will contain the main body of the page. ```Go Template {{template "layout.html" .}} {{define "title"}}Regions{{end}} {{define "content"}} ``` -------------------------------- ### Go HTML Template for Written Contents Page Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/writtencontents.html This snippet defines a complete Go HTML template for rendering a 'Written Contents' page. It extends a base layout ('layout.html'), sets the page title, and defines the main content block. It iterates over provided data to display content details like names, sizes, and a count for each category. It also includes a call to a 'writtenContent' function, passing an ID. ```Go Template {{template "layout.html" .}} {{define "title"}}Written Contents{{end}} {{define "content"}} ### Written Contents {{- range $t, $v := .}} {{$t}} ({{ len $v }}) {{- end}} {{- range $v }}{{- if not (eq .Name "") }} {{- end}}{{- end}} Name Size {{ writtenContent .Id }} {{- end}} {{- end }} ``` -------------------------------- ### Rendering Poetic Form Details - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artforms.html This Go template snippet handles the 'PoeticForms' collection. It includes placeholder loops and then outputs static 'Name' and 'Size' headers, followed by a call to the 'poeticForm' function, passing the 'Id' of the current poetic form. The 'active' variable is reset after the range, likely for UI state management. ```Go Template {{ $active := " active"}} {{- range $t, $v := .PoeticForms }} {{- range $v }}{{- if not (eq .Name "") }} {{- end}}{{- end}} Name Size {{ poeticForm .Id }} {{ $active = ""}}{{- end}} ``` -------------------------------- ### Including Events Template (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/collectionDetail.html This snippet directly includes the `events.html` template, passing the `events` context and the current `.Event` data to it. This allows for modular rendering of event details. ```Go Template {{ template "events.html" events .Event }} ``` -------------------------------- ### Including Base Layout in Go HTML Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/event.html This snippet demonstrates how to include a base layout template, 'layout.html', into the current template. The dot '.' passes the current data context to the included template, allowing the layout to access shared data. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Initializing Variables and Conditional Active Class for Tabs in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/site.html This section initializes a '$history' variable by calling a 'history' function with the site ID and sets an '$active' variable to ' active'. It then conditionally removes the ' active' class if certain data (structures, site properties, or history) is present, likely for dynamic tab navigation styling. ```Go Template {{ $history := history .Id }} {{ $active := " active"}} {{- if gt (len .Structures) 0 }} Structures {{ $active = ""}}{{- end}} {{- if gt (len .SiteProperties) 0 }} Properties {{ $active = ""}}{{- end}} {{- if gt (len $history) 0 }} History {{ $active = ""}}{{- end}} ``` -------------------------------- ### Including Events Template - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/identity.html This snippet includes another template file named 'events.html'. It passes both the 'events' data and the current page context ('.') to the 'events.html' template for rendering, allowing for modular content display. ```Go Template {{ template "events.html" events . }} ``` -------------------------------- ### Defining Layout and Content Blocks in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/region.html This snippet defines the overall page layout by including 'layout.html', sets the page title dynamically using the 'title' helper function, and marks the beginning of the main content block. ```Go Template {{template "layout.html" .}} {{define "title"}}{{ title .Name }}{{end}} {{define "content"}} ``` -------------------------------- ### Displaying Entity Header and Metadata in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/entity.html This snippet renders the primary header for an entity, including its name, race, type, and conditional details like 'necromancer', 'profession', 'parent entity', 'worship', and 'weapons' for military units. It uses Go template conditionals and functions to format the output. ```Go Template ### {{ title .Name }} {{ .Race }}{{ if .Necromancer}} necromancer{{end}} {{ .Type }} {{- if .Profession }} of {{ .Profession }}s {{- end }} {{- if ne .Parent -1 }} of {{ entity .Parent }} {{- end }} {{- if and (eq .Type "religion") (gt (len .WorshipId) 0) }} centered around the worship of {{ hfList .WorshipId }} {{- end }} {{- if eq .Type "militaryunit" }} {{- if gt (len .WorshipId) 0 }} devoted to the worship of {{ hfList .WorshipId }} {{- end }} {{- if gt (len .WorshipId) 0 }} , dedicated to the mastery of {{ andList .Weapons }} {{- end }} {{- end }} ``` -------------------------------- ### Including Layout Template in Go HTML Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/river.html This Go template action includes another template file, 'layout.html', passing the current context '.' to it. It's typically used for defining a base page structure and reusing common UI elements. ```Go Template {{template "layout.html" .}} ``` -------------------------------- ### Displaying Site Properties Content in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/site.html This section conditionally displays site properties if available. It iterates through '.SiteProperties' and shows the property type, and either the associated structure or the property type itself. It also displays the owner's name using the 'hf' function or 'no owner' if not specified. ```Go Template {{- if gt (len .SiteProperties) 0 }} {{- range .SiteProperties }} {{- end}} {{ if ne .StructureId -1 }}{{ structure $.Id .StructureId }}{{else}}{{ .Type_ }}{{end}} {{ if ne .OwnerHfid -1 }}{{ hf .OwnerHfid }}{{else}}no owner{{ end }} {{ $active = ""}}{{- end}} ``` -------------------------------- ### Rendering Dance Form Details - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artforms.html This Go template snippet iterates through the 'DanceForms' collection. It includes placeholder loops and then outputs static 'Name' and 'Size' headers, followed by a call to the 'danceForm' function, passing the 'Id' of the current dance form. The 'active' variable is reset after the range, likely for UI state management. ```Go Template {{ $active := " active"}} {{- range $t, $v := .DanceForms }} {{- range $v }}{{- if not (eq .Name "") }} {{- end}}{{- end}} Name Size {{ danceForm .Id }} {{ $active = ""}}{{- end}} ``` -------------------------------- ### Outputting Current Context as JSON (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/mountain.html This snippet uses the `json` function to serialize the entire current context (`.`) into a JSON string. This can be useful for debugging purposes or for client-side JavaScript consumption of the data. ```Go Template {{ json . }} ``` -------------------------------- ### Displaying Page Title and Debugging with JSON Output Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/landmass.html This snippet re-displays the page title using `title .Name` and then outputs the entire data context (`.`) as a JSON string using `json .`. This is typically used for debugging purposes to inspect the data available to the template. ```Go Template {{ title .Name }} ================= landmass {{ json . }} {{- end }} ``` -------------------------------- ### Creating Dynamic Bootstrap Popovers for Entity Links with AJAX Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/layout.html This snippet defines a function 'loadLinkPopoverData' that fetches popover content via an asynchronous jQuery AJAX request. It then iterates over various types of entity links (e.g., 'a.entity', 'a.hf') and initializes a Bootstrap Popover for each, displaying the fetched content on hover. ```JavaScript function loadLinkPopoverData() { return $.ajax({ url: "./popover" + this.getAttribute("href").substring(1), async: false }).responseText; } $('a.entity,a.hf,a.region,a.site,a.structure,a.worldconstruction,a.artifact,a.writtencontent,a.collection,a.landmass,a.mountain,a.identity,a.river').each(function () { var popover = new bootstrap.Popover($(this), { content: loadLinkPopoverData, trigger: "hover", placement: "top", html: true }) }) ``` -------------------------------- ### Iterating Through Event Collection (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/collectionDetail.html This snippet initiates a range loop over the 'Eventcol' collection. Each iteration processes an individual event, making its data available within the loop's scope for rendering. ```Go Template {{- range .Eventcol }} ``` -------------------------------- ### Displaying Collection and Parent ID in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverCollection.html This Go template snippet renders the current collection's ID using `{{ collection .Id }}`. It then conditionally checks if a parent ID exists (i.e., `ne .ParentId -1`). If a parent exists, it renders the phrase 'as part of' followed by the parent collection's ID using `{{ collection .ParentId }}`. ```Go Template {{ collection .Id }} {{if ne .ParentId -1 }} as part of {{ collection .ParentId }}{{ end }} ``` -------------------------------- ### Defining Main Content Block in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/search.html This snippet defines the 'content' block, which encapsulates the main body of the search results page. All subsequent content within this 'define' and 'end' block will be rendered into the corresponding 'template' action in the 'layout.html'. ```Go Template {{define "content"}} ``` -------------------------------- ### Iterating and Summarizing Collections - Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/artifacts.html This snippet iterates over a collection (likely a map of types to lists of items), assigning the key to `$t` and the value to `$v`. It then prints the type name and the count of items within that type, providing a summary. ```Go Template {{- range $t, $v := .}} {{$t}} ({{ len $v }}) {{- end}} ``` -------------------------------- ### Displaying Character and Entity Position Details (Go Template) Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/popoverHf.html This Go template snippet renders a character's basic attributes like race and supernatural status. It also conditionally displays their birth and death years. Furthermore, it iterates through current and former entity positions, showing the role, associated entity, and tenure. It uses `hf`, `getEntity`, and `entity` functions, and conditional logic with `if`, `or`, `ne`, `len`, `ge`, and `range`. ```Go Template {{ hf .Id }} {{if .Female }} {{else}} {{end}} {{ .Race }} {{ if .Deity}}deity{{end}} {{ if .Force}}force{{end}} {{ if .Vampire}}vampire{{end}} {{ if .Werebeast}}werebeast{{end}} {{ if .Necromancer}}necromancer{{end}} {{ if not (or .Deity .Force)}} (\*{{ .BirthYear }}{{ if ge .DeathYear 0 }} †{{ .DeathYear }}{{ end }}) {{ end }} {{- if or (ne 0 (len .EntityFormerPositionLink)) (ne 0 (len .EntityPositionLink)) }} {{- range $i := .EntityPositionLink }}* {{ ((getEntity $i.EntityId).Position $i.PositionProfileId).GenderName $ }} of {{ entity $i.EntityId }} (since {{ $i.StartYear }}) {{- end }} {{- range $i := .EntityFormerPositionLink }}* {{ ((getEntity $i.EntityId).Position $i.PositionProfileId).GenderName $ }} of {{ entity $i.EntityId }} ({{ $i.StartYear }} - {{ $i.EndYear }}) {{- end }} {{- end }} ``` -------------------------------- ### Displaying Site History Content in Go Template Source: https://github.com/robertjanetzko/legendsbrowser2/blob/master/backend/templates/site.html This snippet checks if historical events exist for the site using the '$history' variable. If events are present, it includes a separate 'events.html' template to render them, passing the events data for display. ```Go Template {{ $history := history .Id }} {{- if gt (len $history) 0 }} {{ template "events.html" events $history }} {{ $active = ""}}{{- end}} ```