glimr/response/view


View Helpers

Builder pattern for rendering views with layouts and template variables. Supports both static HTML files and Lustre components with automatic variable replacement.

Types


View Type

View builder for constructing HTML responses with layouts. Contains the content, layout template, and template variables for dynamic rendering.

pub type View {
  View(
    content: String,
    layout: String,
    data: dict.Dict(String, String),
  )
}

Constructors

  • View(
      content: String,
      layout: String,
      data: dict.Dict(String, String),
    )

Values

pub fn build() -> View

Build View

Creates a new view with empty content, empty layout, and empty template data. Used internally to initialize views.


Example:

view.build()
|> view.html("contact/success.html")
|> view.data([#("title", "My Page")])
|> view.render()
pub fn data(view: View, data: List(#(String, String))) -> View

Add Template Variables

Adds a key-value pair to the template data. Variables are replaced in the layout using {{key}} syntax. The special {{content}} variable is reserved for the main content.


Example:

view.build()
|> view.html("page.html")
|> view.data([
    #("title", "My Page"),
    #("author", "John Doe"),
])
|> view.render()
pub fn error_response(
  status: Int,
  message: String,
) -> response.Response(wisp.Body)

Error Response

Generates an error response with the given HTTP status code. Attempts to load a custom error page from the application’s src/resources/views/errors/{status}.html. If no custom page exists, falls back to the framework’s default error page from the framework’s priv directory with the error.html layout.

This allows applications to override default error pages while maintaining consistent fallback behavior.


Example:

// Returns 404 response with custom or default error page
view.error_response(404, "Page Not Found")

// Returns 500 response with custom or default error page
view.error_response(500, "Internal Server Error")
pub fn html(view: View, file_path: String) -> View

Load HTML File

Creates a view from a static HTML file. The file path is relative to src/resources/views/ and leading slashes are automatically stripped. Panics if the file doesn’t exist.


Example:

view.build()
|> view.html("contact/success.html")
|> view.render()
pub fn html_raw(view: View, content: String) -> View

Load Raw HTML Content

Sets the view content directly from a string without reading from a file. Useful for rendering complete HTML documents or when the HTML is already loaded in memory.


Example:

let html = "<h1>Hello World</h1>"
view.build()
|> view.html_raw(html)
|> view.render()
pub fn layout(view: View, layout_path: String) -> View

Set Layout Template

Sets a custom layout template for the view. The layout path is relative to src/resources/views/layouts/ and leading slashes are stripped. Panics if layout file doesn’t exist.


Example:

view.build()
|> view.html("dashboard.html")
|> view.layout("admin.html")
|> view.render()
pub fn lustre(view: View, content: element.Element(msg)) -> View

Load Lustre Component

Creates a view from a Lustre Element by converting it to an HTML string. Use for interactive components rendered on the server side.


Example:

view.build()
|> view.lustre(contact_form.view(model))
|> view.render()
pub fn render(view: View) -> response.Response(wisp.Body)

Render View

Converts the view builder into an HTTP response. Replaces {{content}} with the content, substitutes all template variables, and removes any unused {{variables}}.


Example:

view.build()
|> view.html("contact/form.html")
|> view.render()
pub fn render_with_status(
  view: View,
  status: Int,
) -> response.Response(wisp.Body)

Render View With Status

Converts the view builder into an HTTP response. Replaces {{content}} with the content, substitutes all template variables, and removes any unused {{variables}}, while also allowing you to set a custom status code like 404/405


Example:

view.build()
|> view.html("contact/form.html")
|> view.render()
Search Document