[text][/text]

Q&A's, tips, howto's
Locked
eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

[text][/text]

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

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

Eddie

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

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

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

Correction! Sometimes I do foolish things.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Lutz

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

Works the same way with 0 or 512.

Eddie

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post 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

Locked