Dragonfly templates | Dragonfly web framework


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:


Into a request for this file:


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: <% (date) %>

That, however, will result in a page that says:

The time is:

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: <% (print (date)) %>
The time is: Thu Sep 20 07:36:20 2018

You can append an equals sign after the OPEN_TAG to achieve the same result in fewer keystrokes:

The time is: <%= (date) %>

You can use all functions from newLISP and Dragonfly (api), as well as functions provided by plugins.


Below is a list of configuration parameters that you'll find in config.lsp related to templates.


Default value: "<%"


Default value: "%>"


Default value: (string DOCUMENT_ROOT "/views")

The location of views (for use with 'display-view' function).


Default value: (string DOCUMENT_ROOT "/views/partials")

The location of partials (for use with 'display-partial' function).


Default value: "welcome"

The default view (when a user visits '/') sans file 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.


Rendered in 3 milliseconds. Used 84 KB of memory, 59 KB for Lisp Cells.