From 5b1dacb6a177bd5a4db41652a080083984a2912a Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Sun, 24 May 2020 07:33:46 +0530 Subject: [PATCH] feat: Set base_template for routes by regex The default `base_template` for any web route was `templates/web.html` by default. Now, you can set the `base_template` for routes by a regex pattern via hooks (using the key `base_template_map`). For e.g, you can set `templates/doc.html` for routes that match the pattern `docs.*` --- frappe/hooks.py | 6 ++++++ frappe/website/context.py | 2 +- frappe/website/router.py | 39 +++++++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/frappe/hooks.py b/frappe/hooks.py index 200280f6de..9c63f05b99 100644 --- a/frappe/hooks.py +++ b/frappe/hooks.py @@ -56,6 +56,12 @@ website_route_rules = [ {"from_route": "/profile", "to_route": "me"}, ] +base_template = "templates/base.html" + +base_template_map = { + r".*": "templates/web.html" +} + write_file_keys = ["file_url", "file_name"] notification_config = "frappe.core.notifications.get_notification_config" diff --git a/frappe/website/context.py b/frappe/website/context.py index 5663199545..9d0f3d2067 100644 --- a/frappe/website/context.py +++ b/frappe/website/context.py @@ -120,7 +120,7 @@ def build_context(context): # determine templates to be used if not context.base_template_path: app_base = frappe.get_hooks("base_template") - context.base_template_path = app_base[0] if app_base else "templates/base.html" + context.base_template_path = app_base[-1] if app_base else "templates/base.html" if context.title_prefix and context.title and not context.title.startswith(context.title_prefix): context.title = '{0} - {1}'.format(context.title_prefix, context.title) diff --git a/frappe/website/router.py b/frappe/website/router.py index 1e999c1611..c915578300 100644 --- a/frappe/website/router.py +++ b/frappe/website/router.py @@ -277,9 +277,12 @@ def setup_source(page_info): if not page_info.show_sidebar: source = '
' + source + '
' + if not page_info.base_template: + page_info.base_template = get_base_template(page_info.route) + # if only content if page_info.template.endswith('.html') or page_info.template.endswith('.md'): - html = extend_from_base_template(page_info, source) + html = source # load css/js files js, css = '', '' @@ -303,22 +306,23 @@ def setup_source(page_info): # show table of contents setup_index(page_info) -def extend_from_base_template(page_info, source): - '''Extend the content with appropriate base template if required. - - For easy composition, the users will only add the content of the page, - not its template. But if the user has explicitly put Jinja blocks, or tags, - or comment tags like - then the system will not try and put it inside the "web.template" +def get_base_template(path=None): ''' + Returns the `base_template` for given `path`. + The default `base_template` for any web route is `templates/web.html` defined in `hooks.py`. + This can be overridden for certain routes in `custom_app/hooks.py` based on regex pattern. + ''' + if not path: + path = frappe.local.request.path - if (('' not in source) and ('{% block' not in source) - and ('