path parameter
This commit is contained in:
+29
-10
@@ -1,36 +1,55 @@
|
||||
(in-package :web-apps)
|
||||
|
||||
(defun get-product (n)
|
||||
;; Query the DB.
|
||||
(list n (format nil "Product nb ~a" n) 9.99))
|
||||
|
||||
(defun products (&optional (n 5))
|
||||
(loop for i from 0 below n
|
||||
collect (list i
|
||||
(format nil "Product nb ~a" i)
|
||||
9.99)))
|
||||
collect (get-product i)))
|
||||
|
||||
(defvar *server* nil
|
||||
"Server instance (Hunchentoot acceptor).")
|
||||
|
||||
(defparameter *port* 8899 "The application port.")
|
||||
|
||||
(defun render (template &rest args)
|
||||
(apply
|
||||
#'djula:render-template*
|
||||
(djula:compile-string template)
|
||||
nil
|
||||
args))
|
||||
|
||||
(defparameter *template-root* "
|
||||
<title> Lisp web app </title>
|
||||
<body>
|
||||
<ul>
|
||||
<ul>
|
||||
{% for product in products %}
|
||||
<li> {{ product.1 }} - {{ product.2 }} </li>
|
||||
<li>
|
||||
<a href=\"/product/{{ product.0 }}\">{{ product.1 }} - {{ product.2 }}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</body>
|
||||
")
|
||||
|
||||
(defun render-products ()
|
||||
(djula:render-template*
|
||||
(djula:compile-string *template-root*)
|
||||
nil
|
||||
:products (products)))
|
||||
(defparameter *template-product* "
|
||||
<body>
|
||||
{{ product }}
|
||||
</body>
|
||||
")
|
||||
|
||||
(easy-routes:defroute root ("/") ()
|
||||
(render *template-root* :products (products)))
|
||||
|
||||
(easy-routes:defroute product-route ("/product/:n") (&path (n 'integer))
|
||||
(render *template-product* :product (get-product n)))
|
||||
|
||||
(defroute root ("/" :method :get) ()
|
||||
(render-products))
|
||||
|
||||
|
||||
|
||||
(defun start-server (&key (port *port*))
|
||||
(format t "~&Starting the web server on port ~a~&" port)
|
||||
(force-output)
|
||||
|
||||
Reference in New Issue
Block a user