mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-15 14:36:07 +00:00

Update to Zmpl v2, update demo app to be compatible with v2 syntax. Add deprecation warning for v1 (v1 is default for now - will force v2 soon).
43 lines
1.9 KiB
Zig
43 lines
1.9 KiB
Zig
const std = @import("std");
|
|
const jetzig = @import("jetzig");
|
|
|
|
/// `src/app/views/root.zig` represents the root URL `/`
|
|
/// The `index` view function is invoked when when the HTTP verb is `GET`.
|
|
/// Other view types are invoked either by passing a resource ID value (e.g. `/1234`) or by using
|
|
/// a different HTTP verb:
|
|
///
|
|
/// GET / => index(request, data)
|
|
/// GET /1234 => get(id, request, data)
|
|
/// POST / => post(request, data)
|
|
/// PUT /1234 => put(id, request, data)
|
|
/// PATCH /1234 => patch(id, request, data)
|
|
/// DELETE /1234 => delete(id, request, data)
|
|
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
|
// The first call to `data.object()` or `data.array()` sets the root response data value.
|
|
// JSON requests return a JSON string representation of the root data value.
|
|
// Zmpl templates can access all values in the root data value.
|
|
var root = try data.object();
|
|
|
|
// Add a string to the root object.
|
|
try root.put("welcome_message", data.string("Welcome to Jetzig!"));
|
|
|
|
// Request params have the same type as a `data.object()` so they can be inserted them
|
|
// directly into the response data. Fetch `http://localhost:8080/?message=hello` to set the
|
|
// param. JSON data is also accepted when the `content-type: application/json` header is
|
|
// present.
|
|
const params = try request.params();
|
|
|
|
try root.put("message_param", params.get("message"));
|
|
|
|
// Set arbitrary response headers as required. `content-type` is automatically assigned for
|
|
// HTML, JSON responses.
|
|
//
|
|
// Static files located in `public/` in the root of your project directory are accessible
|
|
// from the root path (e.g. `public/jetzig.png`) is available at `/jetzig.png` and the
|
|
// content type is inferred from the extension using MIME types.
|
|
try request.response.headers.append("x-example-header", "example header value");
|
|
|
|
// Render the response and set the response code.
|
|
return request.render(.ok);
|
|
}
|