Views
Views return HTML data from your application. They can be created from pure HTML documents or passed through renderers such as Mustache or Stencil.
Views Directory
Views are stored in Resources/Views. This directory can be accessed via the views directory property on the application. This builds the path to the view directory from the working directory.
app.viewsDir
HTML
Returning HTML, or any other non-rendered document, is simple. Just pass the path to the document to View(path: String). This path is relative to viewsDir.
app.get("/") { request in
return try View(path: "index.html")
}
Throws
This call throws a
View.Error.InvalidPathif it cannot find the file. You can catch this in the closure, or in middleware.
Public Resources
Any resources that your views need, such as images, styles, and scripts, should be placed in the Public folder at the root of your application.
View Renderer
Any class that conforms to ViewRenderer can be set to render views with a given context.
class MustacheRenderer: RenderDriver {
...
}
View.renderers[".mustache"] = MustacheRenderer()
This will pass any files that end with .mustache through the mustache renderer before returning them.
Available Renderers
These renderers can be added to your application through Providers.
Mustache
Stencil
Updated less than a minute ago
