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, default 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 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 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()
Search Document