An even simpler example application

24 views
Skip to first unread message

Nick Dimiduk

unread,
Mar 12, 2008, 2:57:33 AM3/12/08
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

Reply all
Reply to author
Forward
0 new messages