from simpleweb import controller, template class IndexPage(object): @controller.publish def index(self): return template.render('index.html') controller.attach('/', IndexPage())
from simpleweb import controller, template class PathElementsPage(object): @controller.publish def default(self, *params): template_args = {'current_path': '/'.join(params), 'next_path': str(len(params) + 1)} print '/'.join(params) return template.render('path_elements.html', **template_args) controller.attach('/path_elements/', PathElementsPage())
def default(self, *args): source_name = 'index' if len(args) == 0 else args[0] if not re.match('^([a-z_])+$', source_name): return filename = join(dirname(__file__), source_name+'.py') if not exists(filename): return with open(filename) as file: code = file.read() source = '<b>controllers/%s</b>' % basename(filename) source += highlight(code, PythonLexer(), HtmlFormatter(noclasses=True)) source += self._dump_views_matching_regex("template.render\('([a-z_]*).", code) controller.set_response('Content-Type', 'text/html') return template.render('view_source.html', source_name=source_name, content=source) def _dump_views_matching_regex(self, regex, data): source = '' for source_name in re.findall(regex, data): filename = join(dirname(__file__), '..', 'views', source_name+'.html') if exists(filename): with open(filename) as file: code = file.read() source += '<b>views/%s</b>' % basename(filename) source += highlight(code, MakoHtmlLexer(), HtmlFormatter(noclasses=True)) # Displaying sub-templates makes the output too noisy, let's keep it commented #source += self._dump_views_matching_regex('%inherit file="([a-z_]+).', code) return source controller.attach('/view_source/', ViewSourcePage())
## -*- coding: utf-8 -*- from simpleweb import controller, template from math import sqrt class FormPage(object): @controller.publish def index(self, **kwargs): # Form input fields are received as keyword arguments if controller.method() == "GET": return template.render('form.html') else: # For the template we provide the input variables extended with the sqrt result kwargs['number_sqrt'] = str(sqrt(float(kwargs['number']))) return template.render('form_submit.html', **kwargs) controller.attach('/form/', FormPage())
## -*- coding: utf-8 -*- from simpleweb import controller, template class HelloPage(object): @controller.publish def index(self): return template.render("hello.html", word="world!") controller.attach("/hello/", HelloPage())
## -*- coding: utf-8 -*- from simpleweb import controller, template class HelloFiltersPage(object): @controller.publish def index(self): return template.render('hello_filters.html', name='Simple+Easy&Nice') controller.attach('/hello_filters', HelloFiltersPage())
## -*- coding: utf-8 -*- from simpleweb import controller, template class GetParamsPage(object): @controller.publish def index(self, name=None): upper_name = name.upper() return template.render('get_params.html', show_name=upper_name) controller.attach('/get_params/', GetParamsPage())