Dragonfly templates | Dragonfly web framework
Templates
In Dragonfly, templates are files that have newLISP code embedded in them. This code is evaluated server-side, generating a final document that's presented to visitors.
Templates can be anything, but usually they're html files with newLISP code embedded in them. When you download Dragonfly, take a look at the files in example-site/views, those are all templates, but they're also referred to as views.
The term "view" comes from the V in MVC (model-view-controller), a paradigm that Dragonfly follows only loosely (notably missing the 'model' aspect).
Views are typically used to represent the data of a model, but Dragonfly doesn't really care to enforce this (although you are certainly welcome to). In this document the term 'view' will be used to refer to templates in the directory specified by DF:VIEWS_PATH.
Where routes come into the picture
Route.Static (one of the built-in routes) contains by default two request transformations for handling views:
(string VIEWS_PATH "/" _)
(string VIEWS_PATH "/" _ VIEW_EXTENSION)
The last transformation listed there is responsible for transforming a request to this URL:
http://rundragonfly.com/dragonfly_templates
Into a request for this file:
/home/www/rundragonfly.com/views/dragonfly_templates.html
All of the files handled by Route.Static eventually get passed through the function eval-template, which searches for any newLISP code between OPEN_TAG and CLOSE_TAG and evaluates it.
Writing templates
By default, the OPEN_TAG is <% and the CLOSE_TAG is %>. Let's say we want to create an HTML file that displays the current time on the server, we might go about it like this:
The time is:
That, however, will result in a page that says:
That's because evaluated expressions do not output anything unless they call print or println. We can fix it by rewriting it like so:
The time is:
You can append an equals sign after the OPEN_TAG to achieve the same result in fewer keystrokes:
The time is:
INFORMATION
You can use all functions from newLISP and Dragonfly (api), as well as functions provided by plugins.
Configuration
Below is a list of configuration parameters that you'll find in config.lsp related to templates.
OPEN_TAG
Default value: "<%"
CLOSE_TAG
Default value: "%>"
VIEWS_PATH
Default value: (string DOCUMENT_ROOT "/views")
The location of views (for use with 'display-view' function).
PARTIALS_PATH
Default value: (string DOCUMENT_ROOT "/views/partials")
The location of partials (for use with 'display-partial' function).
DEFAULT_VIEW
Default value: "welcome"
The default view (when a user visits '/') sans file extension.
VIEW_EXTENSION
Default value: ".html"
Used by one of the transformations in Route.Static so that a request to /welcome loads the file /views/welcome.html.