I just implemented my own idea. :) It's a turbogears.flash()
replacement that:
1. handles multiple messages
2. handles multiple classes of messages (info, warning, error), acting
like a logger
3. handles HTML, but only if you explicitly allow it
I tried to keep it as simple as possible. Here's how you use it:
# instantiate a Flash2 object
f2 = Flash2()
# Add some messages!
f2.info("This is the first message. It's an info.")
f2.warning("This is the second message. It's a warning.")
f2.error("This is the third message. It's an error!")
f2.info("This is the fourth message. It's an info.")
# For HTML, add on html=True:
f2.warning("This is the fifth message. It has some <strong>HTML</
strong> in it! <a href='http://www.google.com/'>Google</a> rocks.",
html=True)
If you like screenshots (and who doesn't?):
http://www.anseljh.com/code/flash2.png
Here's the code.
Two classes go in your controllers.py:
--------------
class Flash2Message:
def __init__(self, msg, cls='info', html=False):
self.message = msg
self.css = cls
self.html = html
class Flash2:
def __init__(self):
self.messages = []
self.messages_dict = {'info':[], 'warning':[], 'error':[]}
def add_message(self, msg, cls, html=False):
m = Flash2Message(msg, cls, html)
self.messages.append(m)
self.messages_dict[cls].append(m)
def info(self, msg, html=False):
self.add_message(msg, 'info', html)
def warning(self, msg, html=False):
self.add_message(msg, 'warning', html)
def error(self, msg, html=False):
self.add_message(msg, 'error', html)
--------------
Here's what goes in your template:
--------------
<div py:if="flash2" id="flash2_container">
<div py:for="message in flash2.messages" class="flash2 $
{message.css}">
<span py:if="message.html" py:content="XML(message.message)" />
<span py:if="not message.html" py:content="message.message" />
</div>
</div>
--------------
And here's some CSS to liven things up:
--------------
<style type="text/css">
#flash2_container {
border: 1px dashed #0f0; /* thin green border for debugging */
/* padding: .5em; */
}
#flash2_container .flash2 {
/* styles for all flash2 messages */
border-width: 3px;
border-style: solid;
margin: .5em;
padding: .5em;
}
#flash2_container .error {
background-color: #fdd; /* red */
border-color: #f00;
}
#flash2_container .warning {
background-color: #ffd; /* yellow? */
border-color: #ff0;
}
#flash2_container .info {
background-color: #ddf; /* blue */
border-color: #00f;
}
</style>
--------------
That's it. Implementing globally is left as an exercise for the
reader. :)
On May 18, 12:00 pm, "Lee McFadden" <[EMAIL PROTECTED]> wrote:
> On 5/18/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
>
> > These are good tips. Here's a related suggestion for 2.0: make the
> > flash work like a logger. For example:
>
> > turbogears.flash.info("An informational message...")
> > turbogears.flash.error("A big freakin' error!")
>
> That's a nice syntax. I've created my own version of tg.flash that
> takes the class as a second argument and defaults to whatever I set it
> in my app.cfg, e.g:
>
> myproject.flash("An informational message...")
> myproject.flash("A big freakin' error!", "error")
>
> Still, that alone doesn't solve the issue of adding things like links
> to your flash message. In the end I gave up trying to hack tg.flash
> and cookies to show messages and started using cherrypy.request to
> store status messages. Since the data is still in pure python and
> hasn't required serialisation into a cookie you can store elementtree
> elements for direct output of links etc.
>
> I'm not 100% sure what the best practice is for storing data in
> cherrypy.request but I use it fairly liberally for this kind of thing.
>
> I don't have any code that I can post at the moment as it's too
> tightly tied with the project, but if there's some interest I could
> probably break it out and publish it.
>
> Lee
>
> --
> Lee McFadden
>
> blog:http://www.splee.co.uk
> work:http://fireflisystems.com
> skype: fireflisystems
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---