Turbogears 2 / Dynamically set template

214 views
Skip to first unread message

pkraus

unread,
Jan 20, 2012, 1:10:17 PM1/20/12
to turbo...@googlegroups.com
I have a search controller that has a customer_search method that uses a specific template.

The content is loaded via ajax load calls in may different parts of my application. The controller method is the same as all it does is perform a search and return the results however depending on what part of my application is doing the search I need to change the view of those results. I could do this with a huge single genshi template but for maintenance and code sanity it makes sense to me to just have multiple views running of the single controller method.

   @expose('erp.templates.search.customer_search') <--- I would like to be able to change this
    def customer_search(self,customer_query):
        results = do_search_stuff(customer_query)
        return dict(results = results)
     
For instance depending on where the app is called I might need the results returned via json, or a different genshi template that displays the results in a more meaningful way depending on where the results were called from.

Reading the forum I found a solution but it was for tg1 and based in kid it said to just return tg_template in the dictionary. This doesn't work and it doesn't solve the problem for when would need to return json.

Thoughts?

--Just a suggestion, but there might be some value in having a tg2 and tg1 group so that searching can yield more specific results.

Alessandro Molina

unread,
Jan 20, 2012, 1:33:43 PM1/20/12
to turbo...@googlegroups.com
TurboGears2 exposes both use_custom_format and override_template
functions which can be called inside your controller to override
templates.

I suggest to use a custom_format with use_custom_format which is
cleaner, but both do their job.

> --
> 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/-/_a_TnwwszaYJ.
> 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.

Chris Lambacher

unread,
Jan 20, 2012, 1:20:52 PM1/20/12
to turbo...@googlegroups.com
Should be something like:


from tg.decorators import override_template

@expose('erp.templates.search.customer_search')

def customer_search(self,customer_query):
    results = do_search_stuff(customer_query)
    if some_condition:
        override_template(self, 'erp.templates.search.newtemplate')
    return dict(results = results) 

-Chris

--
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/-/_a_TnwwszaYJ.
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.



--
Christopher Lambacher
ch...@kateandchris.net

Maxim Oganesyan

unread,
Apr 15, 2014, 9:01:45 AM4/15/14
to turbo...@googlegroups.com
Alessandro, Chris, have a nice day.

Tried today to override template. Strange things happened. Code looks like:

----
class AjaxController(BaseController):
    @expose('myproject.templates.ajax.passport') #for some html requests
    @expose('json') #for json requests
    def mymethod(self, id, category='menu'):
        keys = ['passport', 'volumes']
        if category == 'menu':
            return dict(length=len(keys), elements=keys) #returned when /ajax/mymethod/1/menu.json requested, works fine.
        elif category in keys:
            if category == 'pasport':
                ...
                # some stuff with ORM
                ...
                return dict(params=params) #returned when /ajax/mymethod/1/passport requested, works fine.
            elif category == 'volumes':
                override_template(self, 'myproject.templates.ajax.volumes') # 'myproject.templates.ajax.passport' is still exposed
                ...
                # some stuff with ORM
                ...
                return dict(params=params) #returned when /ajax/mymethod/1/volumes requested
----

Looks like I'm doing something wrong. Any idea?
AjaxController linked to RootController using "ajax = AjaxController()". I didn't find any examples of using 'override_template' except Yours. So I am stuck...

Max.

PS: Christopher, I'm sorry. Looks like 1-st time I sent question to Your email.

Maxim Oganesyan

unread,
Apr 15, 2014, 10:09:16 AM4/15/14
to turbo...@googlegroups.com
Well, after half-day of searching I found some example, that pointed to my mistake.
I should point override_template not to controller, but to method. Should be:
override_template(self.mymethod, 'myproject.templates.ajax.volumes')
instead of:
override_template(self, 'myproject.templates.ajax.volumes')

Sorry to trouble.
Max.

Alessandro Molina

unread,
Apr 15, 2014, 10:09:39 AM4/15/14
to TurboGears .
override_template wants the controller method for which to override the template.

In your case should be override_template(AjaxController.mymethod,  'myproject.templates.ajax.volumes')




--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.

To post to this group, send email to turbo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages