Just an idea, if you're about to displease the MVC gods anyway,
another trick is to make a menu controller (this can be a separate
menu app/plugin if you think this breaks MVC just a little too much,
but then you've got to take care of session/request/etc objects), and
then you do something like

@menu
def myfunc():
  ...
  ret dict(this=that)

and in menu

def menu(f):
   ...
   ret = f()
   ret['menu'] = menudata
   return ret

This way the menu won't get generated all the time (as opposed to
putting it in models), which is good if you do a lot of AJAX, and is
perhaps a bit more universal/isolated from other code. I haven't
actually done this myself, so comments are welcome :)


On Feb 11, 3:14 am, Iceberg <iceb...@21cn.com> wrote:
> Thanks for all the meaningful discussion here. I did learn something.
>
> When talking about generating the menu, perhaps you will like my trick
> here.
>    http://groups.google.com/group/web2py/browse_thread/thread/4988b16cb2...
>
> You might already notice that, if your application have only one
> controller (the controllers/default.py), your "menu trick" can just
> appear at the beginning of default.py, so no need to sacrifice a lamb
> to the MVC god for the menu issue. :-)
>
> On Feb 10, 3:54 am, Timothy Farrell <tfarr...@swgen.com> wrote:
>
> > I just looked at your code.  If you're going application-wide.  Why not
> > just put "response.menu = _menu()" in the model file.  That way you
> > won't have to call it in the controller.
>
> > Here's an unrelated Python performance tip...try to avoid using
> > for-loops for building a list.  The list.append() method is somewhat
> > expensive.  Instead we use something called a list-comprehension.  Your
> > code would look like this as a list-comprehension:
>
> > def _menu():
> >     """
> >     Return the menu as a list of lists where each element
> >     takes the form of [strLinkText, boolIsActiveLink, strLinkUrl]
> >     """
> >     m = [[text, URL(r=request) == url, url] for text, url in MENU]
> >     return m
>
> > For small loops (such as this one), no one will notice much difference,
> > but it's good to know this for large loops or for areas that just need
> > to run really fast.  Also faster code uses less energy and therefore
> > does more to save the planet from Republicans.  <g>
>
> > -tim
>
> > Timothy Farrell wrote:
> > > Welcome to Python and web2py!
>
> > > There are two ways of looking at this issue.
>
> > > 1) "Model" refers to execution model upon which the controller is run
> > > and view is rendered.  In this view, it is entirely acceptable to put
> > > these application-global things in a model file.
>
> > > 2) "Model" refers to data model.  In this view, anything other than data
> > > descriptions contained in a model file is a violation of MVC.  This is
> > > the view of MVC purists.
>
> > > I hold view #2.  However, for those of us who hold view #2, web2py
> > > offers no easy way to have application-wide settings without violating
> > > our MVC convictions.  I have reconciled my convictions by creating a
> > > separate "model" file called "settings.py" placed all my execution model
> > > stuff in that.  Thereby sort of maintaining my view of MVC separation
> > > and still having easy access to application-wide settings.
>
> > > I sacrifice a white lamb to the MVC god every month until there is a
> > > better solution.
>
> > > =)
>
> > > -tim
>
> > > hcvst wrote:
>
> > >> Hi,
>
> > >> thanks for the new release and all the effort you put in. Even if I am
> > >> new to web2py and not a seasoned Python programmer yet, the release
> > >> notes sound  very promising and I look forward to experimenting
> > >> further.
>
> > >> My question:
>
> > >> Model files seem such a convenient location to place functionality
> > >> that is common across controllers, though I suspect that it is bad
> > >> style to do so.
>
> > >> For example to create a model file called helper.py with the following
> > >> contents:
>
> > >> ---------------------------------------------------------------------------
> > >>  -------------
> > >> ## Global Configuration
> > >> # Primary navigation menu
> > >> MENU = [["Home",  URL(r=request, c="default", f="index")],
> > >>         ["Forum", URL(r=request, c="forum",   f="index")],
> > >>         ["Test", "http://test";]]
>
> > >> ## Helper functions
> > >> def _menu():
> > >>     """
> > >>     Return the menu as a list of lists where each element
> > >>     takes the form of [strLinkText, boolIsActiveLink, strLinkUrl]
> > >>     """
> > >>     m = []
> > >>     for text, url in MENU:
> > >>         m.append([text, URL(r=request) == url, url]);
> > >>     return m
> > >> ---------------------------------------------------------------------------
> > >>  -------------
>
> > >> This enables me to call from every controller function:
>
> > >> response.menu = _menu()
>
> > >> Just the fact that response is in global scope  here is very
> > >> convenient. However I do not want to violate the MCV pattern, unless
> > >> it is acceptable here. Moving this logic into a separate module is
> > >> akward, as one has to move varibles back and forth and needs to
> > >> restart web2py every so often for changes to the module to take
> > >> effect.
>
> > >> What is best practice, when it comes to sharing code across
> > >> controllers please?
>
> > >> Best regards,
> > >> HC
>
> > --
> > Timothy Farrell <tfarr...@swgen.com>
> > Computer Guy
> > Statewide General Insurance Agency (www.swgen.com)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to