Web Development with "hunchentoot" and "restas": how to return a template string with parameters

115 views Asked by At

The code:

(restas:define-module #:restas.api
  (:use #:cl))

(in-package #:restas.api)

(restas:start '#:restas.api :port 3000)

(restas:define-route main-tr ("/tr" :method :post)
  (let* ((raw-data (hunchentoot:raw-post-data :force-text t))
     (params (json:decode-json-from-string raw-data)))
    "<h1> It Worksasasas! ${{params}} {{params}} <var> params </var> </h1>"))

in Postman gives me:

=> It Worksasasas! ${{params}} {{params}} params

but does not print the values of params in Postman, to print a value of a varible in Postman. What must i do? (check the screen shot of Postman please)Postman details

I need to get the value of the variable not the name.

2

There are 2 answers

0
Gwang-Jin Kim On

In the github page of restas it says:

RESTAS does not come with a templating library. cl-closure-template and HTML-TEMPLATE are two good templating libraries that can be used with RESTAS.

It means you have to use cl-closure-template e.g. if you want to use templating within restas - at least how I understood it...

0
Ehvince On

Use a templating library. Choices: https://github.com/CodyReichert/awesome-cl#html-generators-and-templates

Djula is an HTML based, Django-like template engine. It's the least surprising choice, but you can use s-expression based ones, and others. An overview of Djula and Spinneret: https://vindarel.github.io/cl-cookbook/web.html#templates

You can also format your string with format before sending it back to the browser. Here we use the ~S (structure) directive to print the data structure as is. To print more "humanly", use ~a ("aesthetic"). See https://lispcookbook.github.io/cl-cookbook/strings.html

(format nil "<h1> It works!</h1> Params are: <var> ~S </var>" params)

nil creates and returns a string. format t would print on standard output, and return NIL -so we don't want it here.