1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 23:17:37 +00:00

Added methods to render templates.

This commit is contained in:
Samuel Lai 2011-10-30 18:37:33 +11:00
parent a83bed32b5
commit 97740a206d
6 changed files with 2685 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
from bottle import route, run, static_file
from jinja2 import Environment, PackageLoader
import sys
import os
@ -7,6 +8,9 @@ import os
BOTTLE_ROOT = os.path.abspath(os.path.dirname(sys.argv[0]))
MEDIA_ROOT = os.path.abspath(BOTTLE_ROOT + '/../../media')
# hopefully this is thread-safe; not sure though. Will need to experiment/check.
TEMPLATE_ENV = Environment(loader=PackageLoader('stackdump', 'templates'))
# WEB REQUEST METHODS
# Bottle will protect us against nefarious peeps using ../ hacks.
@ -18,8 +22,32 @@ def serve_static(filename):
def hello():
return "Hello World!"
@route('/')
def index():
return render_template('index.html')
# END WEB REQUEST METHODS
# VIEW HELPERS
def render_template(template_path, context=None):
if not context:
context = { }
context['SETTINGS'] = get_template_settings()
return TEMPLATE_ENV.get_template(template_path).render(**context)
def get_template_settings():
template_settings = { }
keys = settings.get('TEMPLATE_SETTINGS', [ ])
for k in keys:
template_settings[k] = settings.get(k, None)
return template_settings
# END VIEW HELPERS
# INITIALISATION
if __name__ == '__main__':

View File

@ -7,3 +7,12 @@
SERVER_ADAPTER = 'cherrypy'
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8080
# if the website is hosted under a subpath, specify it here. It must end with a
# slash.
APP_URL_ROOT = '/'
# settings that are available in templates
TEMPLATE_SETTINGS = [
'APP_URL_ROOT'
]

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{{ SETTINGS.APP_URL_ROOT }}media/css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="{{ SETTINGS.APP_URL_ROOT }}media/css/main.css" type="text/css" />
<script type="text/javascript" src="{{ SETTINGS.APP_URL_ROOT }}media/js/jquery-1.6.4.js"></script>
{% block extrahead %}{% endblock %}
</head>
<body>
{%- block body %}
{% endblock -%}
</body>
</html>

View File

@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% block title %}Stackdump Home{% endblock %}
{% block body %}
Welcome to stackdump.
{% endblock %}