Skip to content

Commit 2721761

Browse files
committed
Extract common head markup to a dedicated template.
This also moves common js and css out of their corresponding blocks so we can't override them anymore (which I don't think we want to do anyway)
1 parent 3ad85ca commit 2721761

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

src/Extensions/Twig/GetFromFrontendExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public function getFunctions(): array
3030
return [
3131
new TwigFunction('global_nav', [$this, 'globalNav']),
3232
new TwigFunction('lang_nav', [$this, 'langNav']),
33-
new TwigFunction('footer', [$this, 'footer'])
33+
new TwigFunction('footer', [$this, 'footer']),
34+
new TwigFunction('common-head', [$this, 'commonHead'])
3435
];
3536
}
3637

@@ -69,5 +70,10 @@ public function footer(): string
6970
{
7071
return $this->fromFrontend('/_fragments/footer/');
7172
}
73+
74+
public function commonHead(): string
75+
{
76+
return $this->fromFrontend('/_fragments/common-head/');
77+
}
7278
}
7379

templates/_common-head.html.twig

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<meta charset="utf-8"/>
2+
<meta name="viewport" content="width=device-width, initial-scale=1">
3+
4+
<link rel="home" href="https://www.w3.org/"/>
5+
<link rel="icon" type="image/png" href="https://www.w3.org/assets/logos/w3c/favicon-32.png"/>
6+
<link rel="apple-touch-icon" type="image/png" href="https://www.w3.org/assets/logos/w3c/favicon-180.png"/>
7+
8+
<link rel="stylesheet" href="{{ asset('styles/core.css?ver=1.4', 'website-2021') }}" media="screen"/>
9+
10+
<!--
11+
CSS Mustard Cut
12+
Print (Edge doesn't apply to print otherwise)
13+
Edge, Chrome 39+, Opera 26+, Safari 9+, iOS 9+, Android ~5+, Android UCBrowser ~11.8+
14+
FF 47+
15+
-->
16+
<link rel="stylesheet" id="advanced-stylesheet"
17+
href="{{ asset('styles/advanced.css?ver=1.4', 'website-2021') }}" media="
18+
only print,
19+
only all and (pointer: fine), only all and (pointer: coarse), only all and (pointer: none),
20+
only all and (min--moz-device-pixel-ratio:0) and (display-mode:browser), (min--moz-device-pixel-ratio:0) and (display-mode:fullscreen)
21+
">
22+
<link rel="stylesheet" href="{{ asset('styles/print.css', 'website-2021') }}" media="print"/>
23+
24+
<script src="{{ asset('js/libraries/fontfaceobserver.js', 'website-2021') }}"></script>
25+
26+
<script>
27+
var myFont = new FontFaceObserver('Noto Sans');
28+
29+
Promise.all([myFont.load()]).then(function () {
30+
document.documentElement.className += " fonts-loaded";
31+
});
32+
33+
(function () {
34+
var linkEl = document.getElementById('advanced-stylesheet');
35+
if (window.matchMedia && window.matchMedia(linkEl.media).matches) {
36+
var script = document.createElement('script');
37+
script.src = '{{ asset('js/main.js?ver=1.4', 'website-2021') }}';
38+
script.defer = true;
39+
document.querySelector('head').appendChild(script);
40+
(function (H) {
41+
H.className = H.className.replace(/\bno-js\b/, 'js')
42+
})(document.documentElement);
43+
}
44+
})();
45+
</script>

templates/base.html.twig

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
{%- endblock -%}
4545
</title>
4646

47-
<meta charset="utf-8"/>
48-
<meta name="viewport" content="width=device-width, initial-scale=1">
47+
{{~ include('@W3CWebsiteTemplates/_common-head.html.twig') }}
48+
4949
<meta name="description" content="{% block description %}{{ page.excerpt|default('page.default_description'|trans({}, 'w3c_website_templates_bundle')) }}{% endblock %}"/>
5050
<meta name="thumbnail" content="{{ thumbnail }}"/>
5151
{# <meta property="fb:app_id" content="__hardcoded_fb_app_id__"> #}
@@ -72,52 +72,10 @@
7272
{%- if page.expiryDate is defined and page.expiryDate -%}
7373
<meta name="robots" content="unavailable_after: {{ page.expiryDate }}"/>
7474
{% endif %}
75-
<link rel="home" href="https://www.w3.org/"/>
76-
<link rel="icon" type="image/png" href="https://www.w3.org/assets/logos/w3c/favicon-32.png"/>
77-
<link rel="apple-touch-icon" type="image/png" href="https://www.w3.org/assets/logos/w3c/favicon-180.png"/>
78-
79-
{% block stylesheets %}
80-
<link rel="stylesheet" href="{{ asset('styles/core.css?ver=1.4', 'website-2021') }}" media="screen"/>
81-
82-
<!--
83-
CSS Mustard Cut
84-
Print (Edge doesn't apply to print otherwise)
85-
Edge, Chrome 39+, Opera 26+, Safari 9+, iOS 9+, Android ~5+, Android UCBrowser ~11.8+
86-
FF 47+
87-
-->
88-
<link rel="stylesheet" id="advanced-stylesheet"
89-
href="{{ asset('styles/advanced.css?ver=1.4', 'website-2021') }}" media="
90-
only print,
91-
only all and (pointer: fine), only all and (pointer: coarse), only all and (pointer: none),
92-
only all and (min--moz-device-pixel-ratio:0) and (display-mode:browser), (min--moz-device-pixel-ratio:0) and (display-mode:fullscreen)
93-
">
94-
<link rel="stylesheet" href="{{ asset('styles/print.css', 'website-2021') }}" media="print"/>
95-
{% endblock stylesheets %}
96-
97-
{% block javascripts %}
98-
<script src="{{ asset('js/libraries/fontfaceobserver.js', 'website-2021') }}"></script>
99-
100-
<script>
101-
var myFont = new FontFaceObserver('Noto Sans');
10275

103-
Promise.all([myFont.load()]).then(function () {
104-
document.documentElement.className += " fonts-loaded";
105-
});
76+
{% block stylesheets %}{% endblock stylesheets %}
10677

107-
(function () {
108-
var linkEl = document.getElementById('advanced-stylesheet');
109-
if (window.matchMedia && window.matchMedia(linkEl.media).matches) {
110-
var script = document.createElement('script');
111-
script.src = '{{ asset('js/main.js?ver=1.4', 'website-2021') }}';
112-
script.defer = true;
113-
document.querySelector('head').appendChild(script);
114-
(function (H) {
115-
H.className = H.className.replace(/\bno-js\b/, 'js')
116-
})(document.documentElement);
117-
}
118-
})();
119-
</script>
120-
{% endblock javascripts %}
78+
{% block javascripts %}{% endblock javascripts %}
12179

12280
{% if page.feeds is defined %}
12381
{% for feed in page.feeds %}

0 commit comments

Comments
 (0)