hugo-theme-relearn/exampleSite/content/configuration/customization/dependencies/_index.en.md

63 lines
2.3 KiB
Markdown
Raw Normal View History

2024-09-29 21:48:56 +00:00
+++
description = "Add further code to your site"
2024-10-06 18:37:18 +00:00
options = ["relearn.dependencies"]
2024-10-07 13:30:53 +00:00
title = "Extending Scripts"
weight = 2
2024-09-29 21:48:56 +00:00
+++
2024-10-07 13:30:53 +00:00
A common question is how to add extra CSS styles or JavaScript to your site. This depends on what you need.
2024-10-07 13:30:53 +00:00
## Adding JavaScript or Stylesheets to All Pages
2024-10-07 13:30:53 +00:00
To add JavaScript files or CSS stylesheets to every page, you can include them in `layouts/partials/custom-header.html` or `layouts/partials/custom-footer.html`.
2024-09-29 21:48:56 +00:00
2024-10-07 13:30:53 +00:00
However, this can make your site larger than necessary if these files are only needed on a few pages. The next section explains how to add dependencies only when needed.
2024-09-29 21:48:56 +00:00
2024-10-07 13:30:53 +00:00
## Custom Shortcodes with Dependencies
2024-09-29 21:48:56 +00:00
2024-10-07 13:30:53 +00:00
Some shortcodes need extra JavaScript and CSS files. The theme only loads these when the shortcode is used. You can use this for your own shortcodes too.
2024-09-29 21:48:56 +00:00
2024-10-07 13:30:53 +00:00
For example, to create a shortcode called `myshortcode` that needs the `jquery` library:
2024-09-29 21:48:56 +00:00
2024-10-07 13:30:53 +00:00
1. Create the shortcode file `layouts/shortcodes/myshortcode.html` and add the folloging line somewhere:
2024-09-29 21:48:56 +00:00
````go {title="layouts/shortcodes/myshortcode.html"}
2024-10-07 13:30:53 +00:00
...
{{- .Page.Store.Set "hasMyShortcode" true }}
...
2024-09-29 21:48:56 +00:00
````
2024-10-07 13:30:53 +00:00
2. {{% badge style="cyan" icon="gears" title=" " %}}Option{{% /badge %}} Add this to your `hugo.toml`:
2024-09-29 21:48:56 +00:00
{{< multiconfig file=hugo >}}
[params.relearn.dependencies]
[params.relearn.dependencies.myshortcode]
name = 'MyShortcode'
2024-09-29 21:48:56 +00:00
{{< /multiconfig >}}
2024-10-07 13:30:53 +00:00
3. Create loader file `layouts/partials/dependencies/myshortcode.html`:
2024-09-29 21:48:56 +00:00
````go {title="layouts/partials/dependencies/myshortcode.html"}
{{- if eq .location "footer" }}
<script src="https://www.unpkg.com/jquery/dist/jquery.js"></script>
{{- end }}
````
2024-10-07 13:30:53 +00:00
Important notes:
2024-09-29 21:48:56 +00:00
2024-10-07 13:30:53 +00:00
- Character casing is relevant!
- The `name` in `hugo.toml` must match the `Store` key used in the shortcode file, prefixed with a `has`.
- The key of `relearn.dependencies` must match the loader file name.
2024-09-29 21:48:56 +00:00
2024-10-07 13:30:53 +00:00
See the `math`, `mermaid`, and `openapi` shortcodes for examples.
2024-09-29 21:48:56 +00:00
{{% notice note %}}
2024-10-07 13:30:53 +00:00
For advanced customization, you can use the dependency loader in your own partials:
2024-09-29 21:48:56 +00:00
````go
{{- partial "dependencies.gotmpl" (dict "page" . "location" "mylocation") }}
````
{{% /notice %}}
2024-10-07 13:30:53 +00:00
Give a unique name for the `location` parameter when you call it, so you can distinguish your loaders behavior depending on the location it was called from.