example

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

README

Example Module

A reference implementation demonstrating the oCMS module system. Use this as a template when creating custom modules.

Features

  • Public routes registration
  • Admin routes with dashboard
  • Template functions for use in themes
  • Hook handlers for page events
  • Database migrations
  • Embedded i18n translations
  • CRUD operations example

Routes

Public Routes
Method Path Description
GET /example Public information page
Admin Routes
Method Path Description
GET /admin/example Admin dashboard
GET /admin/example/items List items (JSON)
POST /admin/example/items Create item
DELETE /admin/example/items/{id} Delete item

Template Functions

The module provides template functions for use in themes:

{{ exampleFunc }}      // Returns "Hello from example module"
{{ exampleVersion }}   // Returns module version (e.g., "1.0.0")

Hook Handlers

The module demonstrates how to register hook handlers:

// page.after_save - Triggered after a page is saved
m.ctx.Hooks.Register(module.HookPageAfterSave, module.HookHandler{
    Name:     "example_page_saved",
    Module:   m.Name(),
    Priority: 10,
    Fn: func(ctx context.Context, data any) (any, error) {
        // Handle the event
        return data, nil
    },
})

// page.before_render - Triggered before rendering a page
m.ctx.Hooks.Register(module.HookPageBeforeRender, module.HookHandler{
    Name:     "example_before_render",
    Module:   m.Name(),
    Priority: 5,
    Fn: func(ctx context.Context, data any) (any, error) {
        // Modify render data if needed
        return data, nil
    },
})

Database Schema

The module creates an example items table:

CREATE TABLE example_items (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    description TEXT DEFAULT '',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Module Structure

modules/example/
├── module.go      # Module definition, lifecycle, hooks, migrations
├── handlers.go    # HTTP handlers and database operations
└── locales/       # Embedded i18n translations
    ├── en/messages.json
    └── ru/messages.json

Module Interface Implementation

The example module implements the module.Module interface:

type Module struct {
    module.BaseModule           // Provides Name(), Version(), Description()
    ctx *module.ModuleContext   // Access to DB, Logger, Hooks, Render
}

// Required methods
func (m *Module) Init(ctx *module.ModuleContext) error
func (m *Module) Shutdown() error
func (m *Module) RegisterRoutes(r chi.Router)
func (m *Module) RegisterAdminRoutes(r chi.Router)
func (m *Module) TemplateFuncs() template.FuncMap
func (m *Module) AdminURL() string
func (m *Module) TranslationsFS() embed.FS
func (m *Module) Migrations() []module.Migration

Creating a New Module

  1. Copy this module directory as a template
  2. Rename the package and update module.go
  3. Update routes in RegisterRoutes and RegisterAdminRoutes
  4. Add template functions if needed
  5. Create migrations for your database schema
  6. Add i18n translations in locales/
  7. Register in cmd/ocms/main.go:
import "ocms-go/modules/mymodule"

// In main()
if err := moduleRegistry.Register(mymodule.New()); err != nil {
    return fmt.Errorf("registering mymodule: %w", err)
}

Internationalization

Translations are embedded and automatically loaded. The module supports:

  • English (en)
  • Russian (ru)

Add new languages by creating locales/{lang}/messages.json.

Module Active Status

Modules can be enabled/disabled from Admin > Modules:

  • Active: Routes accessible, appears in sidebar
  • Inactive: Routes return 404 (public) or redirect (admin)

Status is persisted in the database and survives restarts.

Documentation

Overview

Package example provides an example module demonstrating the oCMS module system. This module serves as a reference implementation for creating custom modules.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExamplePage added in v0.9.0

func ExamplePage(pc *adminviews.PageContext, data ExampleViewData) templ.Component

Types

type ExampleItem added in v0.9.0

type ExampleItem struct {
	ID          int64
	Name        string
	Description string
	CreatedAt   string
}

ExampleItem is a view-only struct with pre-formatted date.

type ExampleViewData added in v0.9.0

type ExampleViewData struct {
	Version string
	Items   []ExampleItem
}

ExampleViewData holds data for the example admin page.

type Item

type Item struct {
	ID          int64     `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	CreatedAt   time.Time `json:"created_at"`
}

Item represents an item in the example module.

type Module

type Module struct {
	module.BaseModule
	// contains filtered or unexported fields
}

Module implements the module.Module interface.

func New

func New() *Module

New creates a new instance of the example module.

func (*Module) AdminURL

func (m *Module) AdminURL() string

AdminURL returns the admin dashboard URL for the module.

func (*Module) Init

func (m *Module) Init(ctx *module.Context) error

Init initializes the module with the given context.

func (*Module) Migrations

func (m *Module) Migrations() []module.Migration

Migrations returns database migrations for the module.

func (*Module) RegisterAdminRoutes

func (m *Module) RegisterAdminRoutes(r chi.Router)

RegisterAdminRoutes registers admin routes for the module.

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(r chi.Router)

RegisterRoutes registers public routes for the module.

func (*Module) Shutdown

func (m *Module) Shutdown() error

Shutdown performs cleanup when the module is shutting down.

func (*Module) SidebarLabel

func (m *Module) SidebarLabel() string

SidebarLabel returns the display label for the admin sidebar.

func (*Module) TemplateFuncs

func (m *Module) TemplateFuncs() template.FuncMap

TemplateFuncs returns template functions provided by the module.

func (*Module) TranslationsFS

func (m *Module) TranslationsFS() embed.FS

TranslationsFS returns the embedded filesystem containing module translations.

Jump to

Keyboard shortcuts

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