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.*`
This commit is contained in:
parent
28c70a6aed
commit
5b1dacb6a1
3 changed files with 30 additions and 17 deletions
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -277,9 +277,12 @@ def setup_source(page_info):
|
|||
if not page_info.show_sidebar:
|
||||
source = '<div class="from-markdown">' + source + '</div>'
|
||||
|
||||
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 <body> tags,
|
||||
or comment tags like <!-- base_template: [path] -->
|
||||
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 (('</body>' not in source) and ('{% block' not in source)
|
||||
and ('<!-- base_template:' not in source)) and 'base_template' not in page_info:
|
||||
page_info.only_content = True
|
||||
source = '''{% extends "templates/web.html" %}
|
||||
{% block page_content %}\n''' + source + '\n{% endblock %}'
|
||||
|
||||
return source
|
||||
base_template_map = frappe.get_hooks("base_template_map")
|
||||
patterns = list(base_template_map.keys())
|
||||
patterns_desc = sorted(patterns, key=lambda x: len(x), reverse=True)
|
||||
for pattern in patterns_desc:
|
||||
if re.match(pattern, path):
|
||||
templates = base_template_map[pattern]
|
||||
base_template = templates[-1]
|
||||
return base_template
|
||||
|
||||
def setup_index(page_info):
|
||||
'''Build page sequence from index.txt'''
|
||||
|
|
@ -338,7 +342,10 @@ def load_properties_from_source(page_info):
|
|||
if base_template:
|
||||
page_info.base_template = base_template
|
||||
|
||||
if page_info.base_template:
|
||||
if (page_info.base_template
|
||||
and "{%- extends" not in page_info.source
|
||||
and "{% extends" not in page_info.source
|
||||
and "</body>" not in page_info.source):
|
||||
page_info.source = '''{{% extends "{0}" %}}
|
||||
{{% block page_content %}}{1}{{% endblock %}}'''.format(page_info.base_template, page_info.source)
|
||||
page_info.no_cache = 1
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue