I wanted to display a resultset using the same view template. To be more specific, we can take an online store example:
A navbar will consist of several main links which are categories. A list of subcategory links will show up if you hover over a category link.
When a user clicks on a subcategory, all products associated with that category+subcategory will be displayed in the main html body.
When a user clicks on a category, all subcategories associated with that category will be displayed in the main html body. A user can then click on a subcategory and then it will refer to the scenario above (rendering its respective products).
For a category, this is an example anchor link:
{{=A(
category.name, _href=URL('category_nav_callback', args=1))}} and the resulting URL should be 'default/search_results/category/1'
For a subcategory, this is an example anchor link:
{{=A(
subcategory.name, _href=URL('subcategory_nav_callback', args=[1,1]))}} and the resulting URL should be 'default/search_results/category/1/subcategory/1'
From there, their respective callback functions in the controller would manipulate the database to get an eventual result set.
To display these results, I wanted to use a generic template view (search_results.html) and pass a resultset to it and it would render it on that page.
In the future, I may want to add in new links (like genre or favorites), and then I can reuse the search_results template to display the results.
Maybe I overcomplicated this too much? I could just create individual search pages like this:
{{=A(category.name, _href=URL('searchCategory', args=1))}}
{{=A(category.name, _href=URL('searchSubcategory', args=[1,1]))}}
and then in controllers:
def searchCategory():
# do stuff
return dict(resultSet=resultSet)
def searchSubcategory():
# do stuff
return dict(resultSet=resultSet)
And I could create their views in both searchCategory.html and searchSubcategory.html:
{{for i in resultSet:}}
# do stuff
{{pass}}
But it seems like a lot of repetitive work, although it is probably the easier approach?
On Friday, May 24, 2013 12:33:33 PM UTC-4, Anthony wrote:
It might be helpful if you explain what you're really trying to do. Do you want users to be able to click different links that all point to the /search_results page, but with different types of searches done depending on the link? In that case, why not something like:
{{=A('click for more information', _href=URL("search_results", args=['myCallback', 1]))}}
def search_results():
if request.args(0) == 'myCallback':
resultSet = myCallback(request.args(1))
...
return dict(resultSet=resultSet)
def myCallback(someId):
return db(...)
Of course, in this simple example there's no need for the separate myCallback function, but I assume you want to have multiple such functions with more complexity. The idea is to send all the requests to the search_results function, and use the request.args to identify the type of results, and potentially dispatch to external functions to generate those results.
Anthony
On Friday, May 24, 2013 11:42:46 AM UTC-4,
brac...@gmail.com wrote: