プロジェクト

全般

プロフィール

SourceFourth » 履歴 » バージョン 1

Yuumi Yoshida, 2008-06-29 19:18

1 1 Yuumi Yoshida
* リスト1
2
{{{
3
<html>
4
  <body>
5
    <h2>Asia League Ice Hockey</h2>
6
    <table border="1">
7
    <%25 (for-each (lambda (row) %25>
8
      <tr>
9
       <td><%25= (getter row "no") %25></td>
10
       <td><%25= (getter row "name") %25></td>
11
       <td><%25= (getter row "g") %25></td>
12
      </tr>
13
     <%25 ) res) %25>
14
     </table>
15
  </body>
16
</html>
17
}}}
18
19
* リスト2
20
{{{
21
(define (render path params) 
22
  (let* ((conn (dbi-connect "dbi:sqlite3:alih.db"))
23
         (pos (cgi-get-parameter "pos" params))
24
         (res
25
          (if (and pos (rxmatch #/^[DFG]$/ pos))
26
              (dbi-do conn "SELECT no, name, g FROM player where pos=? order by no"
27
                      '() pos)
28
              (dbi-do conn "SELECT no, name, g FROM player order by no")))
29
         (getter (relation-accessor res)))
30
    (dbi-close conn)
31
    (template "list.tmpl" res getter)))
32
33
(define-macro (template tmpl-file . args)
34
  `(templ-render ,tmpl-file (quote ,args) (list ,@args)))
35
36
(define (templ-render templ-file vars args)
37
  (apply (eval (read-from-string (templ-func templ-file vars)) (interaction-environment))
38
        args))
39
40
(define (templ-func templ-file vars)
41
  (read-from-string 
42
   #`"(lambda ,vars
43
        (let1 port (open-output-string)
44
             ,(expand-templ (file->string templ-file))
45
             (get-output-string port)))"))
46
47
(define (expand-templ templ)
48
  (cond ((#/(.*?)<%25=(.*?)%25>(.*)/ templ) 
49
        => (lambda(m)
50
             #`",(expand-templ (m 1)) (display ,(m 2) port) ,(expand-templ (m 3))"))
51
       ((#/(.*?)<%25(.*?)%25>(.*)/ templ) 
52
        => (lambda(m)
53
             #`",(expand-templ (m 1)) ,(m 2) ,(expand-templ (m 3))"))
54
       (else (format "(display ~s port)" templ))))
55
}}}