[Grok-dev] what is grok.View.flash()?
Martijn Faassen
faassen at startifact.com
Thu Jan 8 15:35:04 EST 2009
Hey,
Douglas Cerna already mentions how it can be used. I'll just dump a bit
of the code I've used:
from zope import component
from z3c.flashmessage.interfaces import IMessageReceiver
class Feedback(grok.View):
grok.context(Interface)
grok.require('zope.Public')
def now(self):
return render_now()
def messages(self):
receiver = component.getUtility(IMessageReceiver)
return [m.message for m in receiver.receive(u'message')]
def errors(self):
receiver = component.getUtility(IMessageReceiver)
return [m.message for m in receiver.receive(u'error')]
with a feedback.pt
<tal:block tal:define="now view/now;">
<ul id="messages"
tal:define="messages python:view.messages()"
tal:condition="messages">
<li tal:repeat="message messages">
<span tal:replace="now"></span>: <span
tal:replace="message"></span>
</li>
</ul>
<ul id="errors"
tal:define="messages python:view.errors()"
tal:condition="messages">
<li tal:repeat="message messages">
<span tal:replace="now"></span>: <span
tal:replace="message"></span>
</li>
</ul>
</tal:block>
You can then use <tal:block replace="structure context/@@feedback" /> at
the top of your page. You can also use this in an ajaxy way (by loading
@@feedback again), by doing an innerHTML replacement of a special area
of your page each time you do something that could cause a feedback update.
I think it would be worthwhile if some of this were more
"out-of-the-boxy", as Brandon's right that all this is not very obvious.
See also the z3c.flashmessage documentation.
Regards,
Martijn
More information about the Grok-dev
mailing list