Nick Dimiduk
unread,Mar 12, 2008, 2:57:33 AM3/12/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to weblocks
Here is a very simple example of a widget, a weblocks Hello World, if
you will. As has been mentioned recently, widgets are essentially
views with state. Perhaps another will find it useful.
Start by creating a webapp named "hello-world", as per the User's
Guide and replace your init-session.lisp with this:
;;; init-session.lisp
(in-package :hello-world)
;; Define our application
(defwebapp 'hello-world
:description "A web application based on Weblocks")
;; Set public files directory to hello-world/pub
(setf *public-files-path* (compute-public-files-path :hello-world))
(defparameter *hello-init-string* "Hello, World!"
"The default value for the text slot in the hello widget.")
(defwidget hello (widget)
((text :accessor hello-text
:initarg :text
:initform *hello-init-string*
:documentation "Text string to display."))
(:documentation "A simple example widget."))
(defun update-hello-text (obj &key value &allow-other-keys)
"A helper function for updating the text of a hello widget.
Could likely be replaced by a lambda in render-widget-body."
(setf (hello-text obj) value))
(defmethod render-widget-body ((obj hello) &rest args)
(declare (ignore args))
(with-html
(:div :class "hello-class"
(:p (str (hello-text obj)))))
;; reset
(when (string/= (hello-text obj) *hello-init-string*)
(render-link (lambda (&rest args)
(declare (ignore args))
(setf (hello-text obj) *hello-init-string*))
(humanize-name "Reset")))
;; new string
(when (string= (hello-text obj) *hello-init-string*)
(with-html-form (:post (metabang.utilities:curry #'update-hello-text
obj))
(:label (:span "New Value: ")
(:input :name "value"
:value (str (hello-text obj))))
(render-button "update-value" :value "Update"))))
;; Define callback function to initialize new sessions
(defun init-user-session (comp)
(setf (composite-widgets comp)
(list (lambda (&rest args)
(declare (ignore args))
(with-html
(:strong "Happy Hacking!")))
(make-instance 'hello :text "Hello New World!"))))
;;; eof