当前位置:编程学习 > 网站相关 >>

用Lisp写网站程序的备忘

1。 使用 hunchentoot 作为网站框架,它的功能基本上类似于Python的web.py

2。 模板使用 html-template 

3。 本实例暂不涉及数据库操作

4。 主要的麻烦是中文的设置

这里给出的是一个最小的可用程序,可以正确处理中文模板文件与中文变量
; 一些辅助函数
(require :asdf)
(defun loadlib (mod)
  (asdf:oos asdf:load-op mod))

(defun reload ()
  (load "web.lisp"))
(defun restart-web ()
  (progn
    (reload)
    (start-web)))

; load 需要的库 
(loadlib :html-template)
(loadlib :hunchentoot)

; 设置 hunchentoot 编码
(defvar *utf-8* (flex:make-external-format :utf-8 :eol-style :lf))
(setq hunchentoot:*hunchentoot-default-external-format* *utf-8*)
; 设置url handler 转发表
(push (hunchentoot:create-prefix-dispatcher "/hello" hello) hunchentoot:*dispatch-table*)
       
; 页面控制器函数
(defun hello ()
  (setf (hunchentoot:content-type*) "text/html; charset=utf-8")
  (with-output-to-string (stream)
    (html-template:fill-and-print-template
     #p"index.tmpl"
     (list :name "Lisp程序员")
     :stream stream)))
; 启动服务器
(defun start-web (&optional (port 4444))
  (hunchentoot:start (make-instance hunchentoot:acceptor :port port)))
模板 index.tmpl

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test Lisp Web</title>
  </head>
  <body>
    <h1>Lisp web开发实例</h1>
    hi, <!-- TMPL_VAR name -->
  </body>
</html>

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,