How can I show response.flash messages with different styles?

943 views
Skip to first unread message

User

unread,
Feb 12, 2014, 8:54:46 PM2/12/14
to web...@googlegroups.com
I am using bootstrap 2.3.2 and I would like to use Bootstrap css alert classes to style response.flash.  These include:
alert
alert-error
alert-success
alert-info

Is there an easy way to specify the type of flash from the controller? 
I'm thinking I could do something like:

response.flash_style = 'alert-success'


and then:

     {{if response.flash:}}
       
<div class="alert{{=response.flash_style or ' alert-info'}}">
       
<button type="button" class="close" data-dismiss="alert">&times;</button>
       
{{=response.flash}}
       
</div>
     
{{pass}}  



Richard Vézina

unread,
Feb 12, 2014, 9:09:37 PM2/12/14
to web2py-users
It too bad, I am sure there were an example of that in the book, but I just can find it back...

:(

Richard


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Richard Vézina

unread,
Feb 12, 2014, 9:11:14 PM2/12/14
to web2py-users
At least you can better understand how it works here : http://web2py.com/books/default/chapter/29/11/jquery-and-ajax?search=error#web2py_ajax-html

Maybe what I was searching for is web2py slice...

If I find it will post the pointer here...

Richard

Anthony

unread,
Feb 12, 2014, 9:36:58 PM2/12/14
to web...@googlegroups.com
Does your solution below not work?

User

unread,
Feb 12, 2014, 9:42:48 PM2/12/14
to web...@googlegroups.com
It works, just looking for best practices.  Curious if anyone had anything really slick.  Not too crazy about adding on the fly attribute to response object, but perhaps a slightly more mangled name would make me more comfortable with that (Although chance of collision seems pretty small and if it were to happen would be easy enough to change)

Cliff Kachinske

unread,
Feb 12, 2014, 9:48:34 PM2/12/14
to web...@googlegroups.com
response.flash = SPAN('flash message goes here', _class='whatever')


On Wednesday, February 12, 2014 8:54:46 PM UTC-5, User wrote:

Anthony

unread,
Feb 12, 2014, 10:14:09 PM2/12/14
to
On Wednesday, February 12, 2014 9:48:34 PM UTC-5, Cliff Kachinske wrote:
response.flash = SPAN('flash message goes here', _class='whatever')

And since this case involves a div and a button, to avoid having to repeat all that every time, just write a function that takes a message and a flash type and generates the appropriate helper object:

def flash(message, type='info'):
   
return DIV(BUTTON(XML('&times;'), _type='button', _class='close', data=dict(dismiss='alert')),
               message
, class='alert alert-%s' % type)

response.flash = flash('You did it!', 'success')

Anthony

User

unread,
Feb 13, 2014, 2:45:17 PM2/13/14
to web...@googlegroups.com
I like this, but will it play nicely with built-in flash messages?  Seems like if you get a web2py built in flash message (e.g. "logged in","logged out") it will come out with no styling since the styling is now in the function rather than the layout?  Although there aren't so many built-in messages that you couldn't override them so probably not that big of an issue.


On Wednesday, February 12, 2014 10:13:03 PM UTC-5, Anthony wrote:
On Wednesday, February 12, 2014 9:48:34 PM UTC-5, Cliff Kachinske wrote:
response.flash = SPAN('flash message goes here', _class='whatever')

And since this case involves a div and a button, to avoid having to repeat all that every time, just write a function that takes a message and a flash type and generates the appropriate helper object:

def flash(message, type='info'):
   
return DIV(BUTTON(XML('&times;'), _type='button', _class='close', data=dict(dismiss='alert')),
               message
, class='alert alert-%s' % type)

response.flash = flash('You did it!', 'success')

Anthony

 
On Wednesday, February 12, 2014 8:54:46 PM UTC-5, User wrote:

Anthony

unread,
Feb 13, 2014, 3:25:24 PM2/13/14
to web...@googlegroups.com
On Thursday, February 13, 2014 2:45:17 PM UTC-5, User wrote:
I like this, but will it play nicely with built-in flash messages?  Seems like if you get a web2py built in flash message (e.g. "logged in","logged out") it will come out with no styling since the styling is now in the function rather than the layout?  Although there aren't so many built-in messages that you couldn't override them so probably not that big of an issue.

If you want to be able to rely on the built-in flash messages, the original approach is probably better.

Anthony

samuel bonill

unread,
Feb 13, 2014, 9:42:47 PM2/13/14
to web...@googlegroups.com

look, Admin-plus (A-PLUS) use a jquery plugin for do that A-PLUS
Reply all
Reply to author
Forward
0 new messages