How can I do this? I’m only wanting to load some JS if on /community/ pages for example. The following does not load the scripts on /community/*** pages
{{ if in .RelPermalink "/community/" }}
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '2345234523452345',
autoLogAppEvents : true,
xfbml : true,
version : 'v17.0'
});
};
</script>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>
{{ end }}
The following does work, but it loads the script on every page including the main / root page:
{{ if ne .RelPermalink "/" }}
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v17.0&appId=2345234523452345&autoLogAppEvents=1" nonce="eBFOWRiZ"></script>
{{ end }}
I’m not sure what to do here. What conditions should I use?
When I {{ .RelParmaLink}} in my partial, the page outputs this on home page:
/
The following condition should only work when RelPermaLink is not equal to /. Yet it outputs the script tags on every page including the home page /.
{{ if ne .RelPermalink "/" }}
{{ end }}
When I go to the community pages, and dump .RelParmaLink, I get: /community/belgium/. The following condition should only render script tags on /community/ pages, yet those tags don’t get rendered.
Another way to do it is if all pages begin with /community/**, then you can try changing {{ if in .RelPermalink "/community/" }} to {{ if hasPrefix .RelPermalink "/community/" }}. That’s how I got mine to work for certain pages.
I think another way could be to have some front matter, and if the front matter contains some parameter, then load some js.
Another method could be to simply just put the JS in the actual layouts/section/community.html template so we don’t need to worry about a condition, but if you need to tackle multiple sections, you’d have to duplicate code, so it’s probably best as a condition.