DEV Community

vindarel
vindarel

Posted on

Common Lisp HTML templates: using Djula in .lisp files

;;;
;;; How to use Djula templates in-file, without separate file templates.
;;;
;;; This can make it easier to ship your app as a single binary.
;;; Otherwise, see
;;; https://lisp-journey.gitlab.io/blog/lisp-for-the-web-build-standalone-binaries-foreign-libraries-templates-static-assets/#embed-html-djula-templates-in-your-binary
;;;
;;; We use two Djula functions:
;;; - compile-string
;;; - render-template*
;;;
;;;

#++
(ql:quickload '("djula" "pythonic-string-reader"))

(pythonic-string-reader:enable-pythonic-string-syntax)

(defparameter *template-hello* """
{% if foo %}
 foo is true
{% else %}
 foo is false
{% endif %}
""")

(defun render (s &rest keys)
  (apply
   #'djula:render-template*
   (djula:compile-string s)
   nil
   ;; key arguments:
   keys
   ))

#++
(render *template-hello* :foo t)
;; =>
" foo is true "
Enter fullscreen mode Exit fullscreen mode

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more