From 9239d2c627d9fa61ff763cdd2819deb2db4732bf Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 4 May 2014 16:01:17 +0530 Subject: [PATCH] Migrate from Cheetah to Django template system Compiled templates are an unnecessary pain in maintance and packaging. If each module is to bring its own templates, compiling them in the build process becomes unnecessarily more complex. The current state of template mess can somewhat be attributed to this. Cheetah only partially supports dynamic templates. It does not support inheritence of dynamic templates. From its documentation: "There is no support for extending from a class that is not imported; e.g., from a template dynamically created from a string. Since the most practical way to get a parent template into a module is to precompile it, all parent templates essentially have to be precompiled." --- plinth.py | 16 ++-- templates/{base.tmpl => base.html} | 117 ++++++++++++++--------------- templates/err.html | 7 ++ templates/err.tmpl | 23 ------ templates/login_nav.html | 13 ++++ templates/login_nav.tmpl | 31 -------- templates/two_col.html | 24 ++++++ templates/two_col.tmpl | 25 ------ util.py | 24 +++--- 9 files changed, 122 insertions(+), 158 deletions(-) rename templates/{base.tmpl => base.html} (56%) create mode 100644 templates/err.html delete mode 100644 templates/err.tmpl create mode 100644 templates/login_nav.html delete mode 100644 templates/login_nav.tmpl create mode 100644 templates/two_col.html delete mode 100644 templates/two_col.tmpl diff --git a/plinth.py b/plinth.py index 2d10aa089..c453787e0 100755 --- a/plinth.py +++ b/plinth.py @@ -3,6 +3,7 @@ import os, stat, sys, argparse from gettext import gettext as _ import cfg +import django.conf if not os.path.join(cfg.file_root, "vendor") in sys.path: sys.path.append(os.path.join(cfg.file_root, "vendor")) @@ -178,13 +179,18 @@ def setup(): cherrypy.engine.signal_handler.subscribe() def main(): - # Initialize basic services - service.init() + # Initialize basic services + service.init() - setup() + setup() - cherrypy.engine.start() - cherrypy.engine.block() + # Configure Django + directory = os.path.dirname(os.path.abspath(__file__)) + templates_directory = os.path.join(directory, 'templates') + django.conf.settings.configure(TEMPLATE_DIRS=(templates_directory,)) + + cherrypy.engine.start() + cherrypy.engine.block() if __name__ == '__main__': main() diff --git a/templates/base.tmpl b/templates/base.html similarity index 56% rename from templates/base.tmpl rename to templates/base.html index ebfe48db6..182fe0f81 100644 --- a/templates/base.tmpl +++ b/templates/base.html @@ -1,10 +1,3 @@ -#def default($text, $default) -#if $text - $text -#else - $default -#end if -#end def @@ -31,22 +24,22 @@ - + - $default($title, "FreedomBox Dashboard") + {% if title %} {{ title }} {% else %} FreedomBox Dashboard {% endif %} - + - - - + + + - + - + - $css + {{ css|safe }} - - - $main_menu_js - $sub_menu_js + + + {{ main_menu_js|safe }} + {{ sub_menu_js|safe }} @@ -114,9 +107,9 @@ - FreedomBoxFreedomBox Dashboard - #block add_nav_and_login - #end block add_nav_and_login + FreedomBoxFreedomBox Dashboard + {% block add_nav_and_login %} + {% endblock %} @@ -124,35 +117,35 @@
- #if $nav or $sidebar_right or $sidebar_left + {% if nav or sidebar_right or sidebar_left %}
- #if $nav + {% if nav %} - #end if - #if $sidebar_left + {% endif %} + {% if sidebar_left %} - #end if - #if $sidebar_right + {% endif %} + {% if sidebar_right %} - #end if + {% endif %}
- #end if + {% endif %}

- #block title_block - $title - #end block title_block + {% block title_block %} + {{ title }} + {% endblock %}

- #block main_block - $main - #end block main_block + {% block main_block %} + {{ main|safe }} + {% endblock %}
@@ -160,44 +153,44 @@
-

#block footer_block +

{% block footer_block %}

Plinth is © Copyright 2012 James Vasile. It is free software offered to you under the terms of the GNU Affero General Public License, Version 3 or later. This Plinth theme was built by Sean "Diggity" O'Brien.

-

Current page: $current_url

+

Current page: {{ current_url }}

- #end block footer_block

+ {% endblock %}

- + - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - $js + + {{ js|safe }} diff --git a/templates/err.html b/templates/err.html new file mode 100644 index 000000000..5426f3659 --- /dev/null +++ b/templates/err.html @@ -0,0 +1,7 @@ +{% extends "login_nav.html" %} + +{% block title_block %} +Error: {{ title }} +
+
+{% endblock %} diff --git a/templates/err.tmpl b/templates/err.tmpl deleted file mode 100644 index 02106184b..000000000 --- a/templates/err.tmpl +++ /dev/null @@ -1,23 +0,0 @@ -#extends templates.login_nav - -#def title_block -Error: $title -
-
-#end def - -#def sidebar_left_block -$sidebar_left -#end def - -#def main_block -$main -#end def - -#def sidebar_right_block -$sidebar_right -#end def - -#def nav_block -$nav -#end def diff --git a/templates/login_nav.html b/templates/login_nav.html new file mode 100644 index 000000000..39459e2c7 --- /dev/null +++ b/templates/login_nav.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block add_nav_and_login %} +