customizing flash()

56 views
Skip to first unread message

Sean DiZazzo

unread,
Aug 30, 2011, 8:32:23 PM8/30/11
to TurboGears
I finally figured out that I could customize the flash() css by
setting flash.static_template in my controllers.

It works great...except for when flash messages are created by the
predicates. Unfortunately, the predicate.has_permission() function,
does not accept a real flash object, so it must be creating it
somewhere along the way with a hard-coded value. Because of that
though, it's not picking up on the right css class. It is probably
sending the generic 'warning' along automatically, but my css is now
looking for a class called 'attention'.

What is the suggested way for customizing the flash css?

~Sean

Christoph Zwerschke

unread,
Aug 31, 2011, 7:54:28 AM8/31/11
to turbo...@googlegroups.com
Am 31.08.2011 02:32 schrieb Sean DiZazzo:
> I finally figured out that I could customize the flash() css by
> setting flash.static_template in my controllers.

You can customize the flash *HTML* by setting flash.static_template, but
this is rarely needed. To customize the *CSS*, you just have to add css
rules for "#flash" and maybe different rules for "#flash .info", "#flash
.warning" and "#flash .error" classes etc. as you like.

> It works great...except for when flash messages are created by the
> predicates. Unfortunately, the predicate.has_permission() function,
> does not accept a real flash object, so it must be creating it
> somewhere along the way with a hard-coded value.

Yes, the repoze.what authorization errors are hardcoded with 'error' or
'warning' status class. Other authorization errors use the status
attribute of the erros instance.

-- Christoph

Sean DiZazzo

unread,
Aug 31, 2011, 1:32:40 PM8/31/11
to TurboGears
Thanks Christoph. Sorry for confusing things by saying
flash.static_template is css. Obviously it is html.

I'm not great at css, but I think I see how I could have used it more
to my advantage. I guess I didn't think of changing the css to match
what TG does. For some reason, my thinking went the other way
around.

I am adding a clickable element inside the flash div for closing the
notification, so I had to add that to the flash html by modifying the
template. I also have a new notification called "information" that
isn't built into TG. Does passing the second argument to flash() just
pass that right along as the css class? If that is true, then I could
have just created the "information" css class and then started passing
that argument in to get the desired effect.

Thanks for your help.

~Sean

Christoph Zwerschke

unread,
Aug 31, 2011, 1:53:17 PM8/31/11
to turbo...@googlegroups.com
Am 31.08.2011 19:32 schrieb Sean DiZazzo:
> I am adding a clickable element inside the flash div for closing the
> notification, so I had to add that to the flash html by modifying the
> template.

Right, that would be a good reason to change the template. But usually
in such cases I just add the button via JavaScript. This has the
advantage that if JavaScript is not activated, not button appears as it
would be nonfunctional anyway.

> I also have a new notification called "information" that
> isn't built into TG. Does passing the second argument to flash() just
> pass that right along as the css class?

Exactly. You can name it whatever you like.

-- Christoph

Juparave

unread,
Jul 13, 2012, 10:25:59 PM7/13/12
to turbo...@googlegroups.com
Hello,

I wonder where exactly should I set flash.static_template.

I want to change the template site-wide and so far what I manage to do is to add a declaration for flash.static_template in every controller.

Should I override the class and set the static_template there?

Thanks, I know is an old issue but so far google hasn't been my friend on this.

Michael Pedersen

unread,
Jul 15, 2012, 1:14:25 AM7/15/12
to turbo...@googlegroups.com
Well, thinking about it, you have a few options.

You can override the class, and set the static template. Nothing wrong with this, and could even be preferred, especially on higher traffic sites. It will only ever be handled once, and it's done.

You can add your logic to your base controller (look at myproject.lib.base:BaseController). That method would work, but will incur an overhead on *every* request to your site. Small, but still there.

You can continue as you have been, adding your logic to each controller method. Not very maintainable, and still incurs the penalty.

You can re-check and make sure you actually need to be doing this. Most people, most of the time, should be able to get along with just updating their CSS files, resulting in no changes at all. This is the best, but is not always something that can be done. If you can, though, I would recommend it.

That's really all the options I can come up with. I hope it helps somewhat.

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To view this discussion on the web visit https://groups.google.com/d/msg/turbogears/-/UWw2OeNbdvcJ.
To post to this group, send email to turbo...@googlegroups.com.
To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.



--
Michael J. Pedersen
My Online Resume: http://www.icelus.org/ -- Google+ http://plus.ly/pedersen
Google Talk: m.ped...@icelus.org -- Twitter: pedersentg

Juparave

unread,
Jul 15, 2012, 1:49:23 AM7/15/12
to turbo...@googlegroups.com
Thanks Michael,

What I finally did was to override the class.

I was looking a way to use the pagination styles from the bootstrap library (http://twitter.github.com/bootstrap/components.html#pagination) to accomplish that I would need to paginate.pager return me a '<li> list of link to pages' .

This is the patch

--- ../tg2.1.5env/lib/python2.7/site-packages/WebHelpers-1.3-py2.7.egg/webhelpers/paginate.py 2012-04-27 21:44:36.000000000 -0500
+++ myapp/lib/paginate.py 2012-07-14 00:37:19.000000000 -0500
@@ -504,7 +504,7 @@
         show_if_single_page=False, separator=' ', onclick=None,
         symbol_first='<<', symbol_last='>>',
         symbol_previous='<', symbol_next='>',
-        link_attr={'class':'pager_link'}, curpage_attr={'class':'pager_curpage'},
+        link_attr={'class':'pager_link'}, curpage_attr={'class':'active'},
         dotdot_attr={'class':'pager_dotdot'}, **kwargs):
         """
         Return string with links to other pages (e.g. "1 2 [3] 4 5 6 7").
@@ -748,7 +748,9 @@
         # Create a link to the first page (unless we are on the first page
         # or there would be no need to insert '..' spacers)
         if self.page != self.first_page and self.first_page < leftmost_page:
-            nav_items.append( self._pagerlink(self.first_page, self.first_page) )
+            text = self._pagerlink(self.first_page, self.first_page)
+            ltext = HTML.li(text)
+            nav_items.append( ltext )
 
         # Insert dots if there are pages between the first page
         # and the currently displayed page range
@@ -756,8 +758,9 @@
             # Wrap in a SPAN tag if nolink_attr is set
             text = '..'
             if self.dotdot_attr:
-                text = HTML.span(c=text, **self.dotdot_attr)
-            nav_items.append(text)
+                text = HTML.a(text, href='#')
+                ltext = HTML.li(text, **self.curpage_attr)
+            nav_items.append(ltext)
 
         for thispage in xrange(leftmost_page, rightmost_page+1):
             # Hilight the current page number and do not use a link
@@ -765,12 +768,14 @@
                 text = '%s' % (thispage,)
                 # Wrap in a SPAN tag if nolink_attr is set
                 if self.curpage_attr:
-                    text = HTML.span(c=text, **self.curpage_attr)
-                nav_items.append(text)
+                    text = HTML.a(text, href='#')
+                    ltext = HTML.li(text, **self.curpage_attr)
+                nav_items.append(ltext)
             # Otherwise create just a link to that page
             else:
                 text = '%s' % (thispage,)
-                nav_items.append( self._pagerlink(thispage, text) )
+                ltext = self._pagerlink(thispage, text)
+                nav_items.append( HTML.li(ltext) )
 
         # Insert dots if there are pages between the displayed
         # page numbers and the end of the page range
@@ -778,14 +783,19 @@
             text = '..'
             # Wrap in a SPAN tag if nolink_attr is set
             if self.dotdot_attr:
-                text = HTML.span(c=text, **self.dotdot_attr)
-            nav_items.append(text)
+                text = HTML.a(text, href='#')
+                ltext = HTML.li(text, **self.curpage_attr)
+            nav_items.append(ltext)
 
         # Create a link to the very last page (unless we are on the last
         # page or there would be no need to insert '..' spacers)
         if self.page != self.last_page and rightmost_page < self.last_page:
             nav_items.append( self._pagerlink(self.last_page, self.last_page) )



On Sunday, July 15, 2012 12:14:25 AM UTC-5, Michael Pedersen wrote:
Well, thinking about it, you have a few options.

You can override the class, and set the static template. Nothing wrong with this, and could even be preferred, especially on higher traffic sites. It will only ever be handled once, and it's done.

You can add your logic to your base controller (look at myproject.lib.base:BaseController). That method would work, but will incur an overhead on *every* request to your site. Small, but still there.

You can continue as you have been, adding your logic to each controller method. Not very maintainable, and still incurs the penalty.

You can re-check and make sure you actually need to be doing this. Most people, most of the time, should be able to get along with just updating their CSS files, resulting in no changes at all. This is the best, but is not always something that can be done. If you can, though, I would recommend it.

That's really all the options I can come up with. I hope it helps somewhat.
On Fri, Jul 13, 2012 at 10:25 PM, Juparave <kan...@gmail.com> wrote:
Hello,

I wonder where exactly should I set flash.static_template.

I want to change the template site-wide and so far what I manage to do is to add a declaration for flash.static_template in every controller.

Should I override the class and set the static_template there?

Thanks, I know is an old issue but so far google hasn't been my friend on this.

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To view this discussion on the web visit https://groups.google.com/d/msg/turbogears/-/UWw2OeNbdvcJ.
To post to this group, send email to turbo...@googlegroups.com.
To unsubscribe from this group, send email to turbogears+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.

Michael Pedersen

unread,
Jul 15, 2012, 1:05:31 PM7/15/12
to turbo...@googlegroups.com
Yeah, you definitely needed it. I'm trying to think of a way to use this patch, though. I don't think we should add it to the quickstart, but I also don't think it should be relegated to the mailing list where it can easily get lost.

I'm going to have to find a solution to this somehow. We get good contributions from people like you.

To view this discussion on the web visit https://groups.google.com/d/msg/turbogears/-/q3Ii_diIGQMJ.

To post to this group, send email to turbo...@googlegroups.com.
To unsubscribe from this group, send email to turbogears+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.

Juparave

unread,
Jul 16, 2012, 1:40:44 AM7/16/12
to turbo...@googlegroups.com
Hello Michael,

The patch needs some work, it was done in a hurry to test a possible solution.

Maybe an option flag like as_list_items=True should return the alternate string.

Michael Pedersen

unread,
Jul 16, 2012, 9:56:52 PM7/16/12
to turbo...@googlegroups.com
On Mon, Jul 16, 2012 at 1:40 AM, Juparave <kan...@gmail.com> wrote:
Hello Michael,

The patch needs some work, it was done in a hurry to test a possible solution.

Maybe an option flag like as_list_items=True should return the alternate string.

I don't know. That doesn't seem quite right, either. After all, that would be changing core code to accommodate a style change. Even more importantly, that's not one we *can* control. WebHelpers is its own separate project.
Reply all
Reply to author
Forward
0 new messages