///
This document provides a comprehensive API reference for the Ztack web framework. Signatures are inferred from the project's README and code snippets, as direct access to the source files was not avai
49 views
~49 views from guests
Guest views are estimated from total page views. These include anonymous visitors and users who weren't logged in when they viewed the page.
This document provides a comprehensive API reference for the Ztack web framework. Signatures are inferred from the project's README and code snippets, as direct access to the source files was not available in the provided context.
router.zig)The router.zig module provides type-safe HTTP routing, allowing you to define and dispatch requests to handlers based on method and path.
pub const HttpMethod = enum { GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS };: Defines the standard HTTP methods supported by the router.pub const Router = struct { ... };: Manages the registration and dispatching of HTTP request handlers.
pub fn init(allocator: std.mem.Allocator) Router: Initializes a new Router instance with a given allocator.pub fn deinit(self: *Router) void: Deinitializes the router, releasing its allocated resources.pub fn register(self: *Router, method: HttpMethod, path: []const u8, handler: *const fn(HttpMethod, []const u8, []const u8) ![]const u8) !void: Registers a handler function for a specific HTTP method and path.pub fn dispatch(self: *Router, method: HttpMethod, path: []const u8, body: []const u8) !?[]const u8: Dispatches an incoming HTTP request to the registered handler that matches the method and path, returning an optional response.pub fn parseRequestLine(request_line: []const u8) !?[]const []const u8: Parses an HTTP request line (e.g., "GET / HTTP/1.1") into its method and path components.pub fn htmlResponse(allocator: std.mem.Allocator, content: []const u8) ![]const u8: A helper function to create a standard HTML response.pub fn jsonResponse(allocator: std.mem.Allocator, content: []const u8) ![]const u8: A helper function to create a standard JSON response.pub fn notFoundResponse() ![]const u8: A helper function to create a 404 Not Found HTTP response.html.zig)The html.zig module offers efficient and type-safe builders for generating HTML content, supporting direct streaming and event delegation.
pub const HtmlDocument = struct { ... };: Manages the construction and rendering of an entire HTML page.
pub fn init(allocator: std.mem.Allocator) HtmlDocument: Initializes a new HtmlDocument instance.pub fn build(self: *HtmlDocument, head_elements: []const Element, body_elements: []const Element) ![]const u8: Builds a complete HTML page as a byte slice from arrays of head and body elements.pub fn renderToWriter(self: *HtmlDocument, writer: anytype, head_elements: []const Element, body_elements: []const Element) !void: Renders a complete HTML page directly to a provided writer without intermediate allocations.pub const Element = struct { ... };: Represents a generic HTML element, serving as the return type for all HTML builder functions.pub fn text(content: []const u8) Element: Creates a plain text node element.pub fn div(class_or_id: ?[]const u8, children: []const Element) Element: Creates a <div> element with optional class/ID and child elements.pub fn h1(class: ?[]const u8, children: []const Element) Element: Creates an <h1> element with an optional class and child elements.pub fn h2(class: ?[]const u8, children: []const Element) Element: Creates an <h2> element with an optional class and child elements.pub fn p(class: ?[]const u8, children: []const Element) Element: Creates a <p> element with an optional class and child elements.pub fn span(class: ?[]const u8, children: []const Element) Element: Creates a <span> element with an optional class and child elements.pub fn button(class: ?[]const u8, onclick: []const u8, children: []const Element) Element: Creates a <button> element with an optional class, an inline onclick handler, and child elements.pub fn button_data(class: ?[]const u8, data_on: []const u8, children: []const Element) Element: Creates a <button> element with an optional class, a data-on attribute for event delegation, and child elements.pub fn input(type: []const u8, id: []const u8, class: ?[]const u8) Element: Creates an <input> element with specified type, ID, and optional class.pub fn input_with_value(type: []const u8, id: []const u8, value: []const u8) Element: Creates an <input> element with specified type, ID, and an initial value.pub fn anchor(href: []const u8, class: ?[]const u8, children: []const Element) Element: Creates an <a> (anchor) element with a hyperlink reference, optional class, and child elements.pub fn meta(charset: []const u8) Element: Creates a <meta> element, typically used for character set declaration.pub fn title(children: []const Element) Element: Creates a <title> element with child elements defining the page title.pub fn script(src_or_content: []const u8, is_src: bool) Element: Creates a <script> element, either referencing an external source or embedding inline content.pub const EVENT_DELEGATION_SCRIPT: []const u8: A string constant containing the JavaScript code for client-side event delegation.transpiler.zig)The transpiler.zig module is responsible for converting a subset of Zig code into equivalent JavaScript, enabling frontend logic to be written in Zig.
pub fn transpileZigToJs(zig_code: []const u8) ![]const u8: Transpiles a given byte slice of Zig code into a byte slice of JavaScript code, validating against unsupported Zig features.js.zig)The js.zig module defines the Abstract Syntax Tree (AST) for representing JavaScript code, which is used internally by the transpiler.
pub const JsValue = union { ... };: Represents various JavaScript literal values, such as numbers, strings, booleans, null, and undefined.pub const JsExpression = union { ... };: Represents a JavaScript expression, encompassing identifiers, binary operations, function calls, property access, and more.pub const JsStatement = union { ... };: Represents a single JavaScript statement, including variable declarations, assignments, conditional statements, and loops.pub const JsFunction = struct { ... };: Represents a JavaScript function definition, specifying its name, parameters, and body of statements.dom.zig)The dom.zig module provides an abstraction layer for interacting with the browser's Document Object Model (DOM) from within Zig code, especially when compiled to WebAssembly.
pub fn getElementById(id: []const u8) ?Element: Retrieves a DOM element by its ID, returning an optional element handle.pub fn setInnerHTML(element: Element, html: []const u8) !void: Sets the inner HTML content of a given DOM element.pub fn addEventListener(element: Element, event_type: []const u8, handler: *const fn() void) !void: Attaches an event listener function to a DOM element for a specified event type.pub fn getValue(element: Element) ?[]const u8: Retrieves the current value of an input or form-related DOM element.