Page 1 of 1

[text][/text]

Posted: Tue Nov 02, 2004 5:42 pm
by eddier
What would really be wonderful for us cgi tinkerers would be to evaluate variables and functions within the [text] [/text] tags like

Code: Select all

(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

Posted: Tue Nov 02, 2004 8:32 pm
by Lutz
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

Posted: Tue Nov 02, 2004 8:50 pm
by eddier
Thanks Lutz! This will do fine if I change file-name to a string-var.

Eddie

Posted: Wed Nov 03, 2004 8:43 pm
by eddier
Sometimes I don't just do foolish things. This works maybe a bit faster? I chose the backquotes for less typing.

Code: Select all

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

Code: Select all

(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]

Posted: Wed Nov 03, 2004 8:44 pm
by eddier
Correction! Sometimes I do foolish things.

Posted: Wed Nov 03, 2004 8:49 pm
by Lutz
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

Posted: Wed Nov 03, 2004 8:52 pm
by Lutz
I wonder if you should choose the option 512 for PCRE_UNGREEDY in case you have more places of quoted sections?

Lutz

Posted: Wed Nov 03, 2004 8:59 pm
by eddier
Works the same way with 0 or 512.

Eddie

Posted: Wed Nov 03, 2004 10:02 pm
by eddier
In order to evaluate functions you need to add in the (string function as

Code: Select all

(define (subs w)
  (replace "`(.+?)`" w (string (eval-string $1)) 0))
This allows stuff like numbers and lists to be substituted.

Code: Select all

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

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