# foolin/gin-template architecture

> Architecture diagram of the GitHub repository foolin/gin-template, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/foolin/gin-template
- Repository: https://github.com/foolin/gin-template

## Overview

##### Main Components
- Core Template Engine: This is the heart of the package (e.g., functions in template.go and related code), which handles parsing, layout composition (master/partials), caching, and function map registration.
- Gin Integration Layer: The parts where the project plugs into the Gin framework; that is, the customization of Gin’s HTMLRender (see examples in basic, advance, block, multiple, gorice).
- Template Configuration Module: The configuration structure where parameters like the template root path, file extensions, master layout file, partials, custom functions, and caching settings are defined.
- Resource/Static File Integration: For cases like the go.rice example where static resources and templates are packaged.

##### Representing Relationships and Interactions
- The web client makes HTTP requests to the Gin router.
- The Gin router forwards requests to handler functions which, in turn, invoke the HTMLRender via the Template Engine.
- The Template Engine reads the template files (located under various directories such as “views”, “layouts”, “partials”) based on the provided configuration.
- The Template Engine composes the final HTML response using master layouts along with partials and any custom functions, then returns it to the Gin context.
- In the case of the go.rice integration, an additional layer is shown where a resource packaging mechanism wraps the static resources before serving them.

##### Key Architectural Patterns and Design Principles
- Configurability: Emphasize the role of the TemplateConfig structure that centralizes details like the directory paths, file extensions, and caching flags.
- Extensibility: Show how custom helper functions (via FuncMap) and inclusion syntax ({{include "layouts/footer"}}) enable flexible template composition.
- Middleware/Multiple Engine Support: Especially in the “multiple” example, there is a distinction between frontend and backend template engines. This pattern should be highlighted as part of the design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A("Web Client"):::client
    B("Gin Router/HTTP Server"):::server
    C("Template Configuration Module"):::config
    D["Core Template Engine"]:::engine

    subgraph "Gin Integration"
        E("HTMLRender Handler"):::handler
    end

    subgraph "Template Rendering Core"
        D1("Template Loader"):::loader
        D2("Template Composer"):::composer
        D3("FuncMap/Helper Functions"):::funcmap
    end

    subgraph "Resource Packaging (go.rice)"
        F("go.rice Config"):::gorice
        G("go.rice Resources"):::gorice
    end

    %% Request flow
    A -->|"sends request"| B
    B -->|"routes to"| E
    E -->|"invokes"| D
    D -->|"uses"| D1
    D -->|"uses"| D2
    D -->|"uses"| D3
    C -->|"feeds config"| D
    F -->|"provides resources"| D1
    G -->|"provides resources"| D1
    D -->|"returns HTML"| B
    B -->|"sends response"| A

    %% Click Events
    click D "https://github.com/foolin/gin-template/blob/master/template.go"
    click C "https://github.com/foolin/gin-template/blob/master/template.go"
    click E "https://github.com/foolin/gin-template/tree/master/examples/basic"
    click F "https://github.com/foolin/gin-template/blob/master/supports/gorice/gorice.go"
    click G "https://github.com/foolin/gin-template/tree/master/examples/gorice"

    %% Styles
    classDef client fill:#f9c,stroke:#333,stroke-width:2px;
    classDef server fill:#ccf,stroke:#333,stroke-width:2px;
    classDef engine fill:#cfc,stroke:#333,stroke-width:2px;
    classDef config fill:#fc9,stroke:#333,stroke-width:2px;
    classDef handler fill:#ffc,stroke:#333,stroke-width:2px;
    classDef gorice fill:#9cf,stroke:#333,stroke-width:2px;
    classDef loader fill:#cff,stroke:#333,stroke-width:2px;
    classDef composer fill:#cff,stroke:#333,stroke-width:2px;
    classDef funcmap fill:#cff,stroke:#333,stroke-width:2px;
```
