AlexCouch / pluvo

Pluvo is a modern web framework purely in Gleam built ontop of mist
4 stars 0 forks source link

HTML rendering #9

Open AlexCouch opened 8 months ago

AlexCouch commented 8 months ago

Description

We need a way to easily load and render an HTML file, but the question is: what is the best way to do so?

Middleware

Middleware can be used to customize how HTML is rendered, loaded, modified, etc. This means that something like lustre can be used with a context extension module to produce views with interpolated data (and even htmx).

Example

pub fn view(ctx: Context) -> Response{
  html([attribute("lang", "en")], [
    head([], [
     title([], [text("Lustre Quickstart")])
    ]),
    body([], [
      h1([], [text("Hello, world!")])
    ])
  ]) |> html.respond(ctx)
}

This is, of course, an example. html.respond would simply be a function that uses the current context to render the html as a response using the current context (to preserve cookies, headers, etc). This means that we could use lustre on the server side to do a DSL within the view.

Templates

Templates are a go-to for many frameworks because they offer the flexibility of writing the html in a separate file and simply loading it into a route handler, interpolating the data, then returning it in a response. However, templates can be provided as a middleware as well.

Conclusion

I believe the best way to do so is with middleware. Middleware can have a lot of flexibility. Being able to modify the context for what is desired by the middleware is very powerful, so middleware providing functions to the route handlers to produce html code can be a better way than having html producers built into the framework. Also, both templates and dsl's can be used as middleware.

AlexCouch commented 8 months ago

Another solution to have as a middleware is htmx