( in-package #:cl-user )
( defpackage rails-to-caveman.view
( :use #:cl )
( :import-from #:rails-to-caveman.config
#:*template-directory* )
( :import-from #:caveman2
#:*response*
#:response-headers )
( :import-from #:djula
#:add-template-directory
#:compile-template*
#:render-template*
#:*djula-execute-package* )
( :import-from #:datafly
#:encode-json )
( :export #:render
#:render-json ) )
( in-package #:rails-to-caveman.view )
( djula:add-template-directory *template-directory* )
( defparameter *template-registry* ( make-hash-table :test 'equal ) )
( defun render ( template-path &optional env )
( let ( ( template ( gethash template-path *template-registry* ) ) )
( unless template
( setf template ( djula:compile-template* ( princ-to-string template-path ) ) )
( setf ( gethash template-path *template-registry* ) template ) )
( apply #' djula:render-template*
template nil
env ) ) )
( defun render-json ( object )
( setf ( getf ( response-headers *response* ) :content-type ) "application/json" )
( datafly:encode-json object ) )
;;
;; Execute package definition
( defpackage rails-to-caveman.djula
( :use #:cl )
( :import-from #:rails-to-caveman.config
#:config
#:appenv
#:developmentp
#:productionp )
( :import-from #:caveman2
#:url-for )
( :export #:title! ) ; Added in Chapter 3 (Mockup section).
)
;; in-package and let code added in Chapter 3, also. This is
;; preperation for Switching Layout Templates section.
( in-package #:rails-to-caveman.djula ) ; Make sure this states your app name.
( let ( title )
( defun title! ( &optional sub )
( if sub
( format nil "~@[~A - ~]~:(~A~)"
( setf title sub )
;; Make sure find-system states your app name.
#. ( asdf:coerce-name ( asdf:find-system :rails-to-caveman ) ) )
title ) ) ) ; End of Chapter 3 additional code.
( setf djula:*djula-execute-package* ( find-package :rails-to-caveman.djula ) )
;;; This is a custom filter which is was added in Chapter 3
;;; (Routing). Apparently, this is a filter which is part of the djula
;;; project's TODOs. This filter is used in 'step13.html' (in
;;; /templates directory).
( djula::def-filter :break ( it ) ( cl-ppcre:regex-replace-all #\newline it "<br />" ) )