From 06788762d60cad293c7ad34dc7525bed8aab6da4 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Wed, 22 Jul 2020 10:09:58 +0530 Subject: [PATCH 1/3] fix(website): Properly load colocated javascript and style --- frappe/templates/base.html | 12 ++++++++++-- frappe/website/router.py | 28 +++++++++++++--------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/frappe/templates/base.html b/frappe/templates/base.html index 0b82b3dac2..8c843a44a4 100644 --- a/frappe/templates/base.html +++ b/frappe/templates/base.html @@ -42,7 +42,11 @@ {{ head_include or "" }} {% endblock -%} - {%- block style %}{%- endblock -%} + {%- block style %} + {% if colocated_css -%} + + {%- endif %} + {%- endblock -%} {%- endfor -%} - {%- block script %}{%- endblock %} + {%- block script %} + {% if colocated_js -%} + + {%- endif %} + {%- endblock %} {%- block body_include %}{{ body_include or "" }}{% endblock -%} diff --git a/frappe/website/router.py b/frappe/website/router.py index db7e6f322c..fb9cae7d76 100644 --- a/frappe/website/router.py +++ b/frappe/website/router.py @@ -283,22 +283,20 @@ def setup_source(page_info): # set the source only if it contains raw content html = source - # load css/js files - js, css = '', '' + # load css/js files + js_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.js') + if os.path.exists(js_path): + if not '{% block script %}' in html: + with io.open(js_path, 'r', encoding = 'utf-8') as f: + js = f.read() + page_info.colocated_js = js - js_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.js') - if os.path.exists(js_path): - if not '{% block script %}' in html: - with io.open(js_path, 'r', encoding = 'utf-8') as f: - js = f.read() - html += '\n{% block script %}\n{% endblock %}' - - css_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.css') - if os.path.exists(css_path): - if not '{% block style %}' in html: - with io.open(css_path, 'r', encoding='utf-8') as f: - css = f.read() - html += '\n{% block style %}\n\n{% endblock %}' + css_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.css') + if os.path.exists(css_path): + if not '{% block style %}' in html: + with io.open(css_path, 'r', encoding='utf-8') as f: + css = f.read() + page_info.colocated_css = css if html: page_info.source = html From 2bf77cd82a3c14976cf4fe2fcd4073978338f4f0 Mon Sep 17 00:00:00 2001 From: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> Date: Wed, 22 Jul 2020 10:32:12 +0530 Subject: [PATCH 2/3] style: Fix sider --- frappe/website/router.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frappe/website/router.py b/frappe/website/router.py index fb9cae7d76..9a647e1030 100644 --- a/frappe/website/router.py +++ b/frappe/website/router.py @@ -286,14 +286,14 @@ def setup_source(page_info): # load css/js files js_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.js') if os.path.exists(js_path): - if not '{% block script %}' in html: + if '{% block script %}' not in html: with io.open(js_path, 'r', encoding = 'utf-8') as f: js = f.read() page_info.colocated_js = js css_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.css') if os.path.exists(css_path): - if not '{% block style %}' in html: + if '{% block style %}' not in html: with io.open(css_path, 'r', encoding='utf-8') as f: css = f.read() page_info.colocated_css = css From 8f6329de4953f99637c98c68ad94645560b51a73 Mon Sep 17 00:00:00 2001 From: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> Date: Wed, 22 Jul 2020 11:23:35 +0530 Subject: [PATCH 3/3] refactor: Simplify code --- frappe/website/router.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/frappe/website/router.py b/frappe/website/router.py index 9a647e1030..263d5b0f07 100644 --- a/frappe/website/router.py +++ b/frappe/website/router.py @@ -285,18 +285,16 @@ def setup_source(page_info): # load css/js files js_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.js') - if os.path.exists(js_path): - if '{% block script %}' not in html: - with io.open(js_path, 'r', encoding = 'utf-8') as f: - js = f.read() - page_info.colocated_js = js + if os.path.exists(js_path) and '{% block script %}' not in html: + with io.open(js_path, 'r', encoding = 'utf-8') as f: + js = f.read() + page_info.colocated_js = js css_path = os.path.join(page_info.basepath, (page_info.basename or 'index') + '.css') - if os.path.exists(css_path): - if '{% block style %}' not in html: - with io.open(css_path, 'r', encoding='utf-8') as f: - css = f.read() - page_info.colocated_css = css + if os.path.exists(css_path) and '{% block style %}' not in html: + with io.open(css_path, 'r', encoding='utf-8') as f: + css = f.read() + page_info.colocated_css = css if html: page_info.source = html