git.fiddlerwoaroof.com
Browse code

Adding some utility functions

fiddlerwoaroof authored on 08/08/2016 18:42:43
Showing 3 changed files
... ...
@@ -9,6 +9,7 @@
9 9
                #:serapeum
10 10
                #:ningle
11 11
                #:fwoar.lisputils
12
+               #:spinneret
12 13
                #:cl-mustache)
13 14
   :serial t
14 15
   :components ((:file "package")
... ...
@@ -16,21 +16,24 @@
16 16
                    renders the model picked out by the controller. Normally, this is
17 17
                    specialized using the DEFINE-VIEW macr"))
18 18
 
19
-(defmacro setf1 (&body body)
20
-  "Make setf a bit nicer to use with paredit"
21
-  (list* 'setf (apply #'append body)))
22
-
23 19
 (defmacro defroutes (app &body routes)
24 20
   "Define a set of routes for given paths. the ROUTES parameter expects this format:
25 21
    ((\"/path/to/{route}\" :method :POST) route-callback) the AS-ROUTE macro helps one
26 22
    avoid binding function values to the route for flexibility."
27 23
   (alexandria:once-only (app)
28
-    (list* 'setf1
29
-           (loop for ((target &key method) callback) in routes
30
-                 collect `((ningle:route ,app ,target :method ,(or method :GET)) ,callback)))))
24
+    `(setfs
25
+       ,@(loop for ((target &key method) callback) in routes
26
+               collect `((ningle:route ,app ,target :method ,(or method :GET)) ,callback)))))
31 27
 
32 28
 
33 29
 (defvar *view-name*)
30
+(defun call-current-view (model)
31
+  "Call the currently running view with a new model.
32
+   
33
+   Useful if one view name is specialized many times on different model classes: the controller can
34
+   pass the container and then the view can recall itself on the contained items."
35
+  (view *view-name* model))
36
+
34 37
 (defmacro as-route (name &rest r &key &allow-other-keys)
35 38
   "Create a lambda directing requests to the route for NAME.  This uses the
36 39
    generic function RUN-ROUTE internally whose default implementation relies on
... ...
@@ -91,6 +94,14 @@
91 94
       (mustache:render* template data))))
92 95
 
93 96
 
97
+(defmacro define-spinneret-view (name (model) &body body)
98
+  (let* ((declarations (when (eq (caar body) 'declare) (car body)))
99
+         (body (if declarations (cdr body) body)))
100
+    `(define-view ,name (,model)
101
+       ,declarations
102
+       (spinneret:with-html-string
103
+         ,@body))))
104
+
94 105
 (defmacro mustache ((template lambda-list data) &body body)
95 106
   "Template specifies the template to be render, lambda-list is used to destructure data
96 107
    and body transforms the destructured data into an alist for use in the template"
... ...
@@ -110,3 +121,4 @@
110 121
          (mustache (,template ,lambda-list ,model)
111 122
                    ,@body)))))
112 123
 
124
+
... ...
@@ -1,10 +1,15 @@
1 1
 ;;;; package.lisp
2 2
 
3 3
 (defpackage #:araneus
4
-  (:use #:cl)
5
-  (:export #:defroutes #:as-route #:define-controller #:define-view
6
-           #:controller #:view #:run-route #:mustache-view #:render-mustache
7
-           #:setf1))
4
+  (:use #:cl #:fw.lu)
5
+  (:export #:defroutes  #:as-route  #:define-controller #:define-view    
6
+           #:controller #:view      #:run-route #:mustache-view    
7
+           #:render-mustache        #:define-spinneret-view
8
+           #:setf1      #:call-current-view))
9
+
10
+(defpackage #:araneus.routes
11
+  (:use #:cl #:alexandria #:fw.lu)
12
+  (:export #:route))
8 13
 
9 14
 (defpackage #:araneus.form
10 15
   (:use #:cl)