From 08df330971dc8c2c738069401356b22f568c17e5 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Mon, 15 Jun 2020 04:41:55 +0200 Subject: [PATCH] Add support for page-specific extra scripts The new front matter variable `page_scripts` allows to specify javascript ressources to be loaded in sync or async mode. This allows javascript code to be loaded, that is only required for a small set of pages (e.g. showing a map or a slider animation). --- README.md | 17 +++++++++++++++++ layouts/_default/baseof.html | 2 +- layouts/partials/site-scripts.html | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6dfdedd..cd16b7f 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,23 @@ If you add a key of `show_reading_time` true to either the Config Params, a page Some scripts need to be added within the page head. To add your own scripts to the page head, simply insert them into the `head-additions.html` partial located in the `layouts/partials` folder. +### Adding Scripts to Specific Pages + +Some pages may require extra scripts (e.g. for showing a map or a slider animation). +These can be specified by setting the `page_scripts` value in the front matter. +Its keys `sync` and `async` may contain lists of script locations. + +``` +title: foo +page_scripts: + sync: + - /js/foo.js + - /js/bar.js + async: + - /js/baz.js +``` + + ### Logo You can replace the title of your site in the top left corner of each page with your own logo. To do that put your own logo into the `static` directory of your website, and add the `site_logo` parameter to the site params in your config file. For example: diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index ff53aa9..1d982a9 100755 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -52,6 +52,6 @@ {{ block "main" . }}{{ end }} {{ block "footer" . }}{{ partialCached "site-footer.html" . }}{{ end }} - {{ block "scripts" . }}{{ partialCached "site-scripts.html" . }}{{ end }} + {{ block "scripts" . }}{{ partialCached "site-scripts.html" . (default "" .Params.page_scripts) }}{{ end }} diff --git a/layouts/partials/site-scripts.html b/layouts/partials/site-scripts.html index bd6c7f5..10ab40c 100644 --- a/layouts/partials/site-scripts.html +++ b/layouts/partials/site-scripts.html @@ -2,3 +2,9 @@ {{ with $script.js }} {{ end }} +{{ range .Params.page_scripts.sync }} + +{{ end }} +{{ range .Params.page_scripts.async }} + +{{ end }}