[text][/text]

Started by eddier, November 02, 2004, 09:42:48 AM

Previous topic - Next topic

eddier

What would really be wonderful for us cgi tinkerers would be to evaluate variables and functions within the [text] [/text] tags like



(set 'title "Today")

(pr
[text]Content-type: text/html

<html>
<head>
  <title>#{title}</title>
</head>
<body>
  <p>Today is #{(now)}</p>
</body>
</html>
[/text])

I have done some other stuff like put (const (global '<html>) "<html>n") etc. in the init.lsp and it helps like the following
[code]
(pr cgi-header
  <html>
  <body>
  ...
  </body>
  </html>
)



But I think being able to evaluate stuff in text tags would be much better. Is it possible?



Eddie

Lutz

#1
There is a special function in cgi.lsp called (CGI:put-page somefile.html). This fnction replaces all <% .. %> in the page evaluating the expressions.



You just create a normal HTML page and embed <% (newlisp-expression) %>, the <%, %> tags enclose lisp expressions.



Then your cgi-file contains statements like (CGI:put-page MyPage.html). The advantage of this approach is, that your html page and code are (mostly) separated. Most HTML editors respect <% %> tags because other scripting languages use them too.



All of the newLISP application: ide, blog and wiki use this approach.



Lutz

eddier

#2
Thanks Lutz!  This will do fine if I change file-name to a string-var.



Eddie

eddier

#3
Sometimes I don't just do foolish things. This works maybe a bit faster? I chose the backquotes for less typing.



(define (subs w)
  (replace "`(.+)`" w (eval-string $1) 0))


Example

(setq title "The Big Heading" section "Section I")
(println (subs [text]
<html>
<body>
<h1>`title`</h1>
<h2>`section`</h2>
</body>
</html>[/text]))
=>
<html>
<body>
<h1>The Big Heading</h1>
<h2>Section I</h2>
</body>
</html>




Eddie[/url]

eddier

#4
Correction! Sometimes I do foolish things.

Lutz

#5
Looks like it is time to uddate put-page in cgi.lsp with something similar. The current routine is still from pre regex days in newLISP.



Lutz

Lutz

#6
I wonder if you should choose the option 512 for PCRE_UNGREEDY in case you have more places of quoted sections?



Lutz

eddier

#7
Works the same way with 0 or 512.



Eddie

eddier

#8
In order to evaluate functions you need to add in the (string function as

(define (subs w)
  (replace "`(.+?)`" w (string (eval-string $1)) 0))


This allows stuff like numbers and lists to be substituted.



(println (subs [text]Content-type: text/html

<html>
<body>
<p>1 + 2 + ... + 8 = `(apply + (sequence 1 8))`</p>
</body>
</html>


Eddie