项目作者: fukamachi

项目描述 :
Embeddable HTML templating engine for Common Lisp with JSX-like syntax
高级语言: Common Lisp
项目地址: git://github.com/fukamachi/lsx.git
创建时间: 2018-09-13T20:25:29Z
项目社区:https://github.com/fukamachi/lsx

开源协议:

下载


LSX

Quicklisp dist
Build Status
Coverage Status

Embeddable HTML templating engine with JSX-like syntax.

Usage

  1. (ql:quickload '(:lsx :local-time))
  2. (lsx:enable-lsx-syntax)
  3. <br/>
  4. ;=> #<LSX/HTML:ELEMENT br {1003F98BF3}>
  5. (lsx:render-object <br/> t)
  6. ;-> <br>
  7. ;=> NIL
  8. (lsx:render-object <a href="/hello">Say Hello</a> t)
  9. ;-> <a href="/hello">Say Hello</a>
  10. ;=> NIL
  11. ;;
  12. ;; Embed Lisp code in {}
  13. (lsx:render-object <a href="/hello">Say Hello at {(local-time:now)}</a> t)
  14. ;-> <a href="/hello">Say Hello at 2018-09-14T05:04:55.009102+09:00</a>
  15. ;=> NIL

Defining custom tags

  1. (lsx:deftag welcome (&key name)
  2. <h1>{name}</h1>)
  3. <welcome name="fukamachi"></welcome>
  4. ;=> #<WELCOME {10028D74D3}>
  5. (lsx:render-object <welcome name="fukamachi" ></welcome> t)
  6. ;-> <h1>fukamachi</h1>
  7. ;=> NIL

Defining templates

  1. (lsx:deftemplate default-layout ()
  2. (title body)
  3. (:render
  4. <html>
  5. <head>
  6. <title>{title}</title>
  7. </head>
  8. <body>
  9. {body}
  10. </body>
  11. </html>))
  12. (lsx:deftemplate index-page (default-layout)
  13. ()
  14. (:default-initargs
  15. :title "Index"
  16. :body <h1>Welcome</h1>))
  17. (lsx:render 'index-page)
  18. ;=> "<!DOCTYPE html>
  19. ; <html>
  20. ; <head>
  21. ; <title>Index</title>
  22. ; </head>
  23. ; <body>
  24. ; <h1>Welcome</h1>
  25. ; </body>
  26. ; </html>
  27. ; "

Loading from file

  1. ;; example.lsx
  2. (lambda (&key (name "Guest"))
  3. <html>
  4. <head>
  5. <title>Welcome {name}</title>
  6. </head>
  7. <body>
  8. <div id="main"><h1>Hello</h1><p><a href="/entries">Show Entries</a></p></div>
  9. </body>
  10. </html>)
  1. (lsx:read-lsx-file #P"example.lsx")
  2. ;=> #<FUNCTION (LAMBDA (&KEY :NAME) :IN "~/Programs/lib/lsx/example.lsx") {1005E72B5B}>
  3. (lsx:render-object (funcall * :name "fukamachi") t)
  4. ;-> <!DOCTYPE html>
  5. ; <html>
  6. ; <head>
  7. ; <title>Welcome fukamachi</title>
  8. ; </head>
  9. ; <body>
  10. ; <div id="main"><h1>Hello</h1><p><a href="/entries">Show Entries</a></p></div>
  11. ; </body>
  12. ; </html>
  13. ;=> NIL

How it works

LSX syntax is implemented as reader macro. It’s able to see how it’s expanded with quoting.

  1. '<br/>
  2. ;=> (LSX/TAG:H 'BR (LIST))
  3. '<a href="/hello">Say Hello</a>
  4. ;=> (LSX/TAG:H 'A (LIST (CONS "href" "/hello")) (LIST "Say Hello"))
  5. '<a href="/hello">Say Hello at {(local-time:now)}</a>
  6. ;=> (LSX/TAG:H 'A (LIST (CONS "href" "/hello")) (LIST "Say Hello at " (LAMBDA () (LOCAL-TIME:NOW))))

h is a function to make an element. It takes a single required argument, a tag-name as a string, and 2 optional arguments, attributes as an association list and children as a list of elements.

  1. ;; Same as <br/>
  2. (lsx:h "br")
  3. ;=> #<LSX/HTML:ELEMENT br {10033183D3}>
  4. (lsx:h "a" '(("href" . "/hello")) '("Say Hello"))
  5. ;=> #<LSX/HTML:ELEMENT a {100331D823}>
  6. (lsx:h "a" '(("href" . "/hello")) (list "Say Hello at " (lambda () (local-time:now))))

See Also

Author

Copyright (c) 2018 Eitaro Fukamachi

License

Licensed under the BSD 2-Clause License.