On demand code inject based on Front Matter

If you use different JS, JQ, CSS scripts on specific *.md pages this might be handy to just include scripts where you need them.

On the *.md page I add into front matter i.e.:

scripts: ['yt','js01','jq02']

Then in /partials/ head or footer I add i.e.:

{{ if in .Params.scripts "yt" }}
<script src="/js/lite-youtube-embed.js"></script>
{{ end }}
3 Likes

Good tips! Here is another for including javascript.

If you have a shortcode that needs som javascript you can to this:

{{ if .HasShortcode "contact" -}}
{{ $contact := resources.Get "js/contact.js" | minify | fingerprint -}}
<script defer src="{{ $contact.RelPermalink }}"></script>
{{ end -}}
4 Likes