presentation through functions.

1 view
Skip to first unread message

nunb

unread,
Aug 22, 2008, 7:50:02 AM8/22/08
to weblocks
Is it possible to do something like:

(defclass example ()
((message :accessor get-message :initarg :message)
(messag2 :accessor get-messag2 :initarg :messag2)
(id)))

(defview example-table-view (:type table :inherit-from '(:scaffold
example))
(id :hidep t)
(messag2 :label "shoot" :present-as (funcall #'somefn) ))

Where you can call an arbitrary function with the field value before
rendering?

I'm looking at views/dataview.lisp and there's maybe a promising start
there with a blank class called text-presentation that allows
specializing of render-view-field-value. only I am not sure how the
presentations compiler factors into all of this.

any suggestions?

also, what's the difference between render-view-field-value and print-
view-field-value?

Leslie P. Polzer

unread,
Aug 22, 2008, 10:48:25 AM8/22/08
to weblocks
> (defview example-table-view (:type table :inherit-from '(:scaffold
> example))
>   (id :hidep t)
>   (messag2 :label "shoot" :present-as (funcall #'somefn)  ))
>
> Where you can call an arbitrary function with the field value before
> rendering?

You need to define a new presentation that delegates rendering
to an arbitrary function:

(defclass function-presentation ()
((fn :type function :accessor fn :initarg :fn)))

...
(messag2 :label "shoot" :present-as (function :fn #'somefn) ))
...

Stephen Compall

unread,
Aug 22, 2008, 3:04:28 PM8/22/08
to webl...@googlegroups.com
nunb <nandan.bagchee-Re5...@public.gmane.org> writes:
> any suggestions?

Why do you want to call a function? There are many places to hook in;
your needs determine where to put it.

Pull weblocks-dev and look in contrib/s11001001/presentations.lisp for
an example of subclassing text-presentation to change how something is
rendered.¹ Receiving initializers for your presentation's slots from the
defview form is trivial.

Alternatively, if you are really looking for an observer on the
presentation process, maybe you want an :after method on rvfv, or rvf.

> also, what's the difference between render-view-field-value and print-
> view-field-value?

text-presentation's method for rvfv uses pvfv to stringify the value.
Thus, there is no rvfv for password-presentations and data-views, only
one for form-views: the pvfv method takes care of things for other
views.

¹Oops, just spotted an obvious bug in the parser…

--
I write stuff at http://failex.blogspot.com/ now. But the post
formatter and themes are terrible for sharing code, the primary
content, so it might go away sooner or later.

Vyacheslav Akhmechet

unread,
Aug 22, 2008, 8:54:21 PM8/22/08
to webl...@googlegroups.com
On Fri, Aug 22, 2008 at 7:50 AM, nunb <nandan....@gmail.com> wrote:
> (defview example-table-view (:type table :inherit-from '(:scaffold
> example))
> (id :hidep t)
> (messag2 :label "shoot" :present-as (funcall #'somefn) ))
Eventhough this can't be done now, it's probably a good idea for quick
presentations. I'll try to hack it in at some point, but it's not a
very high priority.

nunb

unread,
Aug 25, 2008, 3:06:30 AM8/25/08
to weblocks
Sorry for not replying earlier, I was away the weekend.This is what
I'm using.

(defclass functioncall-presentation (text-presentation)
((function :accessor get-function :initarg :function)))

(defmethod render-view-field-value (value (presentation functioncall-
presentation)
field view widget obj &rest args
&key highlight &allow-other-keys)
(declare (ignore args highlight))
(if (null value)
(call-next-method)
(with-html
(:span :class "value"
(str (funcall (get-function presentation)
value))
))))


The rationale is for 'quick' mods to objects before rendering. Eg if I
have an employee class with a reference to an employer company then I
can present just the name of the company without creating a mixin and
view. Also I'm using it for random stuff like concatenating first and
last names etc.

I would love to hear better ways of doing this: Stephen said there are
many places to hook in.. for example, could a YUI type rich text
editor fit into a field? (ie when a field is rendered a text editor
with the field contents is displayed).

Stephen> Receiving initializers for your presentation's slots from the
defview form is trivial.

That's good to know. I was afraid I'd have to understand the parser/
compiler, but it looks like the above works without that effort on my
part!

Stephen Compall

unread,
Aug 25, 2008, 4:17:33 AM8/25/08
to webl...@googlegroups.com
nunb <nandan.bagchee-Re5...@public.gmane.org> writes:
> (defclass functioncall-presentation (text-presentation)
> ((function :accessor get-function :initarg :function)))
>
> (defmethod render-view-field-value (value (presentation functioncall-
> presentation)
> field view widget obj &rest args
> &key highlight &allow-other-keys)
> (declare (ignore args highlight))
> (if (null value)
> (call-next-method)
> (with-html
> (:span :class "value"
> (str (funcall (get-function presentation)
> value))

This is definitely a case where you ought to specialize
print-view-field-value instead. It will do all the HTML (and
highlighting, I guess) magic for you; you just answer a string from the
method, which I guess is answered from the function you're calling.

> ))))

Side note on Lisp style: please don't do this :)

> That's good to know. I was afraid I'd have to understand the parser/
> compiler, but it looks like the above works without that effort on my
> part!

I hope that defview's comment was helpful for you.

nunb

unread,
Aug 25, 2008, 5:24:26 AM8/25/08
to weblocks

> > (defmethod render-view-field-value (value (presentation functioncall-
> > presentation)
> >                                     field view widget obj &rest args
> >                                      &key highlight &allow-other-keys)
> >   (declare (ignore args highlight))
> >   (if (null value)
> >       (call-next-method)
> >       (with-html
> >         (:span :class "value"
> >                (str (funcall (get-function presentation)
> >                              value))
>
> This is definitely a case where you ought to specialize
> print-view-field-value instead.  It will do all the HTML (and
> highlighting, I guess) magic for you; you just answer a string from the
> method, which I guess is answered from the function you're calling.

I replaced the above r-v-f-v with this:

(defmethod print-view-field-value (value (p functioncall-presentation)
field view widget obj &rest args)
(declare (ignore obj view field args))
(format nil "XXX ~A XXX" (funcall (get-function p) value)))

but it does not work. I guess I have to specialize value too?

right now weblocks is calling the version in dataview.lisp that is
specialized on (value standard-object) -- which just prints the object-
class-name as a string.

> > ))))
>
> Side note on Lisp style: please don't do this :)

I don't usually. Some text was cut out :)

Stephen Compall

unread,
Aug 25, 2008, 12:44:32 PM8/25/08
to webl...@googlegroups.com
nunb <nandan.bagchee-Re5...@public.gmane.org> writes:
> (defmethod print-view-field-value (value (p functioncall-presentation)
> field view widget obj &rest args)
> (declare (ignore obj view field args))
> (format nil "XXX ~A XXX" (funcall (get-function p) value)))
>
> but it does not work. I guess I have to specialize value too?

Looks like it.

Reply all
Reply to author
Forward
0 new messages