XDPXI's Documentation
HTTP

Content Generation

Required version: Sponge >=1.0.5

http.template(filepath)

Reads and returns the contents of a file. Useful for loading HTML templates.

Parameters:

  • filepath (string) - Path to the template file (e.g., "index.html", "templates/home.html")

Returns: string - The file contents, or an error if the file cannot be read

Example:

let html = http.template("index.html");
http.page("/", html);

Multiple Templates:

let home = http.template("templates/home.html");
let about = http.template("templates/about.html");

http.page("/", home);
http.page("/about", about);

http.json(data)

Converts Sponge data structures to JSON format. Perfect for building APIs.

Parameters:

  • data (any) - A value to convert to JSON (dictionaries, arrays, strings, numbers, booleans, null)

Returns: string - Valid JSON string

Object Example:

let response = { status: "ok", message: "Success" };
http.page("/api/status", http.json(response));

Array Example:

let users = [
    { id: 1, name: "Alice" },
    { id: 2, name: "Bob" },
    { id: 3, name: "Charlie" }
];
http.page("/api/users", http.json(users));

Nested Example:

let response = {
    success: true,
    data: {
        users: [
            { id: 1, name: "Alice" },
            { id: 2, name: "Bob" }
        ],
        total: 2
    }
};
http.page("/api/data", http.json(response));