Consider this piece of code:
class Page(object):
def __init__(self, name, title):
self.name = name
self.title = title
self.selected = False
def select(self): <-- How can I make this method work?
for Page in Pages:
Page.selected = False
self.selected = True
class Website(object):
def __init__(self):
self.index = Page("index", "Home")
self.settings = Page("settings", "Settings")
self.users = Page("users", "Users")
self.logs = Page("logs", "Logs")
self.faq = Page("faq", "FAQ")
def __iter__(self):
return iter([self.index, self.settings, self.users, self.logs, self.faq])
Pages = Website()
What I am trying to do seems sort of illegal. Nevertheless, I am sure there is a way to do it. It seems like I might have to rewrite get method somewhere. Thank you very much for your help!
Here is the way I was intending to use those classes using Bottlepy:
Setting Pages:
@route('/')
@route('/<selectedPage>')
@route('/<selectedPage>/')
def dynamic_routing(selectedPage='index'):
for Page in Pages:
if selectedPage == Page.name:
Page.select()
return template('default')
Retrieving Page info (inside Bottlepy template):
%for Page in Pages:
%if Page.selected:
<title>{{Page.title}}</title>
%else:
<title>Page Not Found</title>
%end
%end
I edited code to a working version now. Thanks everyone for such a fast input!!! You guys rock! Still probably not the best approach but I can not think of another way to solve it at the moment.