hop

package module
v0.0.0-...-13f75ec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2025 License: MIT Imports: 12 Imported by: 0

README


Quickstart

  • go get github.com/hoplang/hop-go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/hoplang/hop-go"
)

const template = `
<function name="main" params-as="items">
	<!DOCTYPE html>
	<html>
	<head>
		<title>Quickstart</title>
	</head>
	<body>
		<for each="items" as="item">
			<div inner-text="item.title"></div>
		</for>
	</body>
	</html>
</function>
`

type Item struct {
	Title string `json:"title"`
}

func main() {
	compiler := hop.NewCompiler()
	compiler.AddModule("main", template)
	program, err := compiler.Compile()
	if err != nil {
		log.Fatalf("Failed to compile: %v", err)
	}
	mux := http.NewServeMux()
	mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
		data := []Item{
			{Title: "foo"},
			{Title: "bar"},
			{Title: "baz"},
		}
		err := program.ExecuteFunction(w, "main", "main", data)
		if err != nil {
			log.Fatalf("Failed to execute: %v", err)
		}
	})
	fmt.Println("Server starting at http://localhost:8089")
	err = http.ListenAndServe(":8089", mux)
	if err != nil {
		log.Fatalf("Failed to compile: %v", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compiler

type Compiler struct {
	// contains filtered or unexported fields
}

func NewCompiler

func NewCompiler() *Compiler

func (*Compiler) AddFS

func (c *Compiler) AddFS(fsys fs.FS) error

func (*Compiler) AddModule

func (c *Compiler) AddModule(moduleName string, template string)

func (*Compiler) Compile

func (c *Compiler) Compile() (*Program, error)

type Program

type Program struct {
	// contains filtered or unexported fields
}

func (*Program) ExecuteFunction

func (p *Program) ExecuteFunction(w io.Writer, moduleName string, functionName string, data any) error

ExecuteFunction executes a specific function from the template with the given parameters

func (*Program) GetModules

func (p *Program) GetModules() map[string][]string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL