Posted to tcl by colin at Thu May 06 04:56:37 GMT 2010view raw

  1. # hello-direct - a hello world example of a Direct domain
  2. namespace eval ::Hello {
  3.  
  4. proc / {r args} {
  5. # this is the default
  6. set content {
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <title>Hello World</title>
  11. </head>
  12. <body>
  13. <h1>Hello World</h1>
  14. <ul>
  15. <li><a href='text'>Plain Text</a></li>
  16. <li><a href='html'>Html Fragment</a></li>
  17. <li><a href='error'>Intentional Error</a></li>
  18. <li><a href='redirect'>Redirection</a></li>
  19. </body>
  20. </html>
  21. }
  22. return [Http Ok $r $content text/html]
  23. }
  24.  
  25. proc /text {r args} {
  26. # [Http Ok] can return other mime types:
  27. # Here, text/plain can be used to return just the literal text
  28. set content {
  29. <p>Hello World</p>
  30. }
  31. return [Http Ok $r $content text/plain]
  32. }
  33.  
  34. proc /html {r args} {
  35. # Here, content is returned as an x-text/html-fragment
  36. # which is wrapped and filled in by the Convert module
  37. # to present an HTML page to the client
  38. set content {
  39. <p>Hello World</p>
  40. }
  41. return [Http Ok $r $content x-text/html-fragment]
  42. }
  43.  
  44. proc /args {r args} {
  45. }
  46.  
  47. proc /error {r args} {
  48. # errors are caught and presented to the client
  49. error "This is an intentional error."
  50. }
  51.  
  52. proc /redirect {r args} {
  53. # You can redirect URLs using the facilities of the Http utility
  54. return [Http Moved $r hello] ;# this redirects you to /hello
  55. }
  56.  
  57. proc new {args} {}
  58.  
  59. namespace export -clear *
  60. namespace ensemble create -subcommands {}
  61. }
  62.  
  63. # this line inserts the Hello namespace into the URL space at /hello/
  64. Nub domain /hello/ Direct namespace ::Hello