common-lispsbclhunchentoot

Hunchentoot/easy-routes - return multiple values to the dom?


I have a :get route with easy-routes that, when hit, runs one function (quick-test). The function quick-test returns two values. Both are strings.

(easy-routes:defroute test-file ("/test-file" :method :get) (&get filename)
  (quick-test filename))

However, when the form is sent to "/test-file", only one line is visible in the dom.

I then tried using multiple value bind. But the result is the same.

(easy-routes:defroute test-file ("/test-file" :method :get) (&get filename)
  (multiple-value-bind (a b) (quick-test filename)
    (values (format nil "~a" a)
        (format nil "~a" b))))

My goal is to be able to run quick test and see all returned values on the dom.

Is there a way to do that?

For completeness, here is the form:

<form id="form" action="/test-file" method="get">
    <input type="text" name="filename" placeholder="type the filename"/>
    <input type="submit" value="Send"/>
  </form>


Solution

  • Values can return more values, but if your function expects some expression, it takes only the first returned value and discards the rest. For example, floor returns two values, but + takes only the first:

    > (floor 5 2)
    2
    1
    
    > (+ 2 (floor 5 2))
    4
    

    This question can also help you: values function in Common Lisp

    Defroute probably does the same and takes only the first value.

    You can fix your route for example like this:

    (easy-routes:defroute test-file ("/test-file" :method :get) (&get filename)
      (multiple-value-bind (a b) (quick-test filename)
        (format nil "~a, ~a" a b)))