MCPcopy Index your code
hub / github.com/abiosoft/mold

github.com/abiosoft/mold @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
76 symbols 280 edges 9 files 16 documented · 21%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Mold

Build Status Test Coverage Go Report Card Package Documentation

Mold builds on Go templates to provide a simple and familiar API for rendering web pages.

Features

Mold offers the following, making it an ideal choice for Go projects.

  • Lightweight: uses only the Go standard library with no external dependencies.
  • Efficient: utilises Go's in-built template parser under the hood.
  • Capable: supports all capabilities of Go templates.
  • Familiar: employs the well-known concepts of Layouts, Views and Partials.

Getting Started

1. Create a view file

Create an HTML file named index.html.

{{define "head"}}
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
{{end}}

<h1>Hello from a <a href="https://github.com/abiosoft/mold/raw/main/github.com/abiosoft/mold">Mold</a> template</h1>

2. Render

Create a new instance and render the view in an HTTP handler.

//go:embed index.html
var dir embed.FS

var engine, _ = mold.New(dir)

func handle(w http.ResponseWriter, r *http.Request){
    engine.Render(w, "index.html", nil)
}

Examples

Check the examples directory for more.

Documentation

Go package documentation is available at https://pkg.go.dev/github.com/abiosoft/mold

Concepts

Layouts

Layouts provide the overall structure for your web pages. They define the common elements that are shared across multiple views, such as headers, footers, navigation menus, stylesheets e.t.c.

Inside a layout, calling render without an argument inserts the view's content into the layout's body. To render a specific section, pass the section's name as an argument.

<!DOCTYPE html>
<html>
<head>
    {{render "head"}}
</head>
<body>
    {{render}}
</body>
</html>

The default layout can be overridden by creating a custom layout file and specifying it as an option for a new instance.

option := mold.WithLayout("path/to/layout.html")
engine, err := mold.New(fs, option)

Views

Views are templates that generate the content that is inserted into the body of layouts. Typically what you would put in the <body> of an HTML page.

<h3>Hello from Mold :)</h3>

The path to the view file is passed to the rendering engine to produce HTML output.

engine.Render(w, "path/to/view.html", nil)

Partials

Partials are reusable template snippets that allow you to break down complex views into smaller, manageable components. They are supported in both views and layouts with the partial function.

Partials are ideal for sharing common logic across multiple views and layouts.

{{partial "path/to/partial.html"}}

An optional second argument allows customizing the data passed to the partial. By default, the view's data context is used.

{{partial "partials/user_session.html" .User}}

Sections

Sections allow content to be rendered in specific parts of the layout. They are defined within views with a define block.

The default layout is able to render HTML content within the <head> tag by utilising the head section.

{{define "scripts"}}
<script src="https://github.com/abiosoft/mold/raw/main/unpkg.com/alpinejs" defer></script>
{{end}}

Why not standard Go templates?

Go templates, while simple and powerful, can feel unfamiliar when dealing with multiple template files.

Mold addresses this by providing an intuitive framework for structuring web applications with Go templates.

License

MIT

Sponsoring

You can support the author by donating on Github Sponsors or Buy me a coffee.

Extension points exported contracts — how you extend this code

Engine (Interface)
Engine represents a web page renderer, incorporating a specific layout. It provides a flexible way to generate HTML by c
mold.go
Option (FuncType)
Option is a configuration option for a new [Engine]. It is passed as argument(s) to [New].
mold.go

Core symbols most depended-on inside this repo

New
called by 24
mold.go
String
called by 15
process.go
Must
called by 12
mold.go
Render
called by 11
mold.go
WithLayout
called by 8
mold.go
hasExt
called by 4
engine.go
newVal
called by 4
engine.go
update
called by 3
engine.go

Shape

Function 57
Struct 7
Method 6
TypeAlias 4
FuncType 1
Interface 1

Languages

Go100%

Modules by API surface

mold_test.go26 symbols
engine.go18 symbols
mold.go14 symbols
process.go11 symbols
examples/2_custom_layout/main.go3 symbols
examples/1_default_layout/main.go2 symbols
process_test.go1 symbols
engine_test.go1 symbols

For agents

$ claude mcp add mold \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact