Widgets
Widget objects should subclasses of reblocks/widget:widget
class
also, minimally you have to define a method for the
reblocks/widget:render
generic-function. This function should return
use reblocks/html:with-html
macro to render the widget.
Other parts of API
around widgets are:
reblocks/dependencies:get-dependencies
- returns a list ofCSS
/JS
dependencies.reblocks/widget:update
- marks a widget as need to update on the frontend.reblocks/widget:get-html-tag
- returns aHTML
tag instead of standardDIV
.reblocks/widget:get-css-classes
- returns a list ofCSS
classes. By default returns:WIDGET
and a widget class's name.reblocks/widget:create-widget-from
- return a widget for representing an object. This way widgets can be created out of strings, functions, etc.
Example
To define a widget, use reblocks/widget:defwidget
macro. It creates a class
with a proper meta-class. Old Weblocks version used this metaclass to
discover changes slots, and probably this feature will be returned back some day.
CL-USER> (reblocks/widget:defwidget hello ()
((name :initarg :name
:reader get-name)))
#<REBLOCKS/WIDGETS/MOP:WIDGET-CLASS COMMON-LISP-USER::HELLO>
CL-USER> (defmethod reblocks/widget:render ((widget hello))
(reblocks/html:with-html ()
(:span ("Hello ~A" (get-name widget)))))
#<STANDARD-METHOD REBLOCKS/WIDGET:RENDER (HELLO) {1004E27BC3}>
Then call this, to run a webserver and preview your widget in the browser:
CL-USER> (reblocks/preview:preview
(make-instance 'hello
:name "Bob"))
A result will look like this:
String widget
This is a simple type of widget which can be made out of any string.
Create it using reblocks/widgets/string-widget:make-string-widget
function.