On Nov 2, 12:40 pm, Christoph Zwerschke <[email protected]> wrote:
> Am 31.10.2011 09:43, schrieb Lingfeng Xiong:
>
> > In the example above, most of the parametered are common, such as
> > 'username', 'codeleft' and 'phonelist'. Is there anyway for me to
> > shorten my return line? Thanks.
>
> Why don't you make a method in your controller, like:
>
> def common_info(self, page, **kw):
> user = sefl.user
> info = dict(page=page,
> username = user.user_name,
> phoneinfo = self.phoneInfo,
> phonelist = PhoneInfo.GetPhoneList(user),
> codeleft = GetCodeLeft(user),
> phonename = PhoneInfo.GetPhoneName(
> user=useruser, imei=self.phoneInfo.IMEI))
> info.update(kw)
> return info
>
> And then in your exposed controller method, you can write:
>
> ...
> return self.common_info('rename')
>
> or
>
> ...
> return self.common_info('otherpage', moreinfo='etc')
>
> Also, you can use the add_global_tmpl_vars function in lib.helpers to
> provide info that will be available in *all* templates globally.
>
> -- Christoph
Similar to Christoph's solution; you could use a decorator...
def common_info(func):
def newfunc(*args, **kwargs):
kwargs["phonelist"] = "..."
kwargs["codeleft"] = "..."
return func(*args, **kwargs)
return newfunc
@common_info
def index(self):
...
~Sean
--
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.