| | 1 | = Migration of Templates to modules/templates = |
| | 2 | [[TOC]] |
| | 3 | |
| | 4 | == Introduction == |
| | 5 | |
| | 6 | We have changed the way templates are handled: |
| | 7 | |
| | 8 | * templates now live in modules/templates (formerly private/templates) |
| | 9 | * the template's config.py is now a Python module instead of a Python script and will be imported rather than executed |
| | 10 | * the settings in config.py have been moved into a function config() |
| | 11 | |
| | 12 | This has a couple of advantages: |
| | 13 | |
| | 14 | * being in the modules-path, templates can be imported and do not need to be re-compiled and executed on every request |
| | 15 | * templates are treated as Python packages so that config.py can import template-specific modules |
| | 16 | * config.py can now define classes and functions without risk for memory leaks |
| | 17 | * templates can share common modules via regular imports |
| | 18 | |
| | 19 | We have implemented a fallback logic for legacy templates, to give downstream projects some time to migrate. We highly recommend to migrate any legacy templates from private/templates to modules/templates. This page gives instructions how to do this. |
| | 20 | |
| | 21 | == Moving to modules/templates == |
| | 22 | |
| | 23 | Copy the entire template folder to modules/templates. |
| | 24 | |
| | 25 | == Refactoring config.py == |
| | 26 | |
| | 27 | Re-structure config.py so that all settings-assignment are inside a function config(): |
| | 28 | |
| | 29 | Old config.py: |
| | 30 | {{{ |
| | 31 | # -*- coding: utf-8 -*- |
| | 32 | |
| | 33 | try: |
| | 34 | # Python 2.7 |
| | 35 | from collections import OrderedDict |
| | 36 | except: |
| | 37 | # Python 2.6 |
| | 38 | from gluon.contrib.simplejson.ordered_dict import OrderedDict |
| | 39 | |
| | 40 | from gluon import current |
| | 41 | from gluon.storage import Storage |
| | 42 | |
| | 43 | T = current.T |
| | 44 | settings = current.deployment_settings |
| | 45 | |
| | 46 | """ |
| | 47 | Template settings |
| | 48 | |
| | 49 | All settings which are to configure a specific template are located here |
| | 50 | |
| | 51 | Deployers should ideally not need to edit any other files outside of their template folder |
| | 52 | """ |
| | 53 | |
| | 54 | settings.base.system_name = "Example" |
| | 55 | settings.base.system_name_short = "Example" |
| | 56 | |
| | 57 | # PrePopulate data |
| | 58 | settings.base.prepopulate = ("default/users",) |
| | 59 | ... |
| | 60 | }}} |
| | 61 | |
| | 62 | New config.py: |
| | 63 | {{{ |
| | 64 | # -*- coding: utf-8 -*- |
| | 65 | |
| | 66 | try: |
| | 67 | # Python 2.7 |
| | 68 | from collections import OrderedDict |
| | 69 | except: |
| | 70 | # Python 2.6 |
| | 71 | from gluon.contrib.simplejson.ordered_dict import OrderedDict |
| | 72 | |
| | 73 | from gluon import current |
| | 74 | from gluon.storage import Storage |
| | 75 | |
| | 76 | def config(settings): |
| | 77 | """ |
| | 78 | Template settings |
| | 79 | |
| | 80 | All settings which are to configure a specific template are located here |
| | 81 | |
| | 82 | Deployers should ideally not need to edit any other files outside of their template folder |
| | 83 | """ |
| | 84 | |
| | 85 | T = current.T |
| | 86 | |
| | 87 | settings.base.system_name = "Example" |
| | 88 | settings.base.system_name_short = "Example" |
| | 89 | |
| | 90 | # PrePopulate data |
| | 91 | settings.base.prepopulate = ("default/users",) |
| | 92 | ... |
| | 93 | }}} |
| | 94 | |
| | 95 | Function and class definitions can (should) still be at the module level, not inside the config() function. |
| | 96 | |
| | 97 | Note that the variable "settings" is passed in as parameter for the config() function - there is no need for {{{settings = current.deployment_settings}}}. |
| | 98 | |
| | 99 | The config() function returns nothing. |
| | 100 | |
| | 101 | == Adapt all Paths inside the Template == |
| | 102 | |
| | 103 | Some template files may still contain paths with "private/templates" (e.g. views/layout.html). Go through all the files and update these paths so that they find the template at the new location. |
| | 104 | |
| | 105 | == Changes in 000_config.py == |
| | 106 | |
| | 107 | In 000_config.py, you can replace this block: |
| | 108 | {{{ |
| | 109 | path = template_path() |
| | 110 | if os.path.exists(path): |
| | 111 | settings.exec_template(path) |
| | 112 | }}} |
| | 113 | |
| | 114 | with just this: |
| | 115 | {{{ |
| | 116 | settings.import_template() |
| | 117 | }}} |
| | 118 | |
| | 119 | The old statement will still work, but contains an unnecessary path check which should be avoided. The function template_path() is deprecated and will be removed over time. |
| | 120 | |
| | 121 | == Fallbacks == |
| | 122 | |
| | 123 | Most core functions have fallback paths for legacy templates and old-style config.py - but this support will be phased out over time. |
| | 124 | |
| | 125 | New core functionality will not support templates at the old location. |
| | 126 | |
| | 127 | It is therefore recommended to migrate all templates. Follow the steps above and/or ask for support on our mailing list to migrate your templates. |
| | 128 | |