<!DOCTYPE html> <html lang="en" dir="ltr" itemscope itemtype="http://schema.org/Article" data-r-output-format="print"> <head> <meta charset="utf-8"> <meta name="viewport" content="height=device-height, width=device-width, initial-scale=1.0, minimum-scale=1.0"> <meta name="generator" content="Hugo 0.145.0"> <meta name="generator" content="Relearn 7.5.0+a5b517d1aadb9c5feb6e5fed9f848527763e35e7"> <meta name="description" content="Add further code to your site"> <meta name="author" content="Sören Weber"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:image" content="https://mcshelby.github.io/hugo-theme-relearn/images/hero.png"> <meta name="twitter:title" content="Adding Scripts :: Hugo Relearn Theme"> <meta name="twitter:description" content="Add further code to your site"> <meta property="og:url" content="https://mcshelby.github.io/hugo-theme-relearn/configuration/customization/extending/index.html"> <meta property="og:site_name" content="Hugo Relearn Theme"> <meta property="og:title" content="Adding Scripts :: Hugo Relearn Theme"> <meta property="og:description" content="Add further code to your site"> <meta property="og:locale" content="en"> <meta property="og:type" content="website"> <meta property="og:image" content="https://mcshelby.github.io/hugo-theme-relearn/images/hero.png"> <meta itemprop="name" content="Adding Scripts :: Hugo Relearn Theme"> <meta itemprop="description" content="Add further code to your site"> <meta itemprop="wordCount" content="360"> <meta itemprop="image" content="https://mcshelby.github.io/hugo-theme-relearn/images/hero.png"> <meta itemprop="keywords" content="Explanation,Howto,Relearn.dependencies"> <title>Adding Scripts :: Hugo Relearn Theme</title> <link href="https://mcshelby.github.io/hugo-theme-relearn/configuration/customization/extending/index.html" rel="alternate" hreflang="x-default"> <link href="https://mcshelby.github.io/hugo-theme-relearn/configuration/customization/extending/index.html" rel="alternate" hreflang="en"> <link href="https://mcshelby.github.io/hugo-theme-relearn/pir/configuration/customization/extending/index.html" rel="alternate" hreflang="art-x-pir"> <link href="https://mcshelby.github.io/hugo-theme-relearn/configuration/customization/extending/index.html" rel="canonical" type="text/html" title="Adding Scripts :: Hugo Relearn Theme"> <link href="/hugo-theme-relearn/configuration/customization/extending/index.xml" rel="alternate" type="application/rss+xml" title="Adding Scripts :: Hugo Relearn Theme"> <link href="/hugo-theme-relearn/images/logo.svg?1742828380" rel="icon" type="image/svg+xml"> <link href="/hugo-theme-relearn/fonts/fontawesome/css/fontawesome-all.min.css?1742828380" rel="stylesheet" media="print" onload="this.media='all';this.onload=null;"><noscript><link href="/hugo-theme-relearn/fonts/fontawesome/css/fontawesome-all.min.css?1742828380" rel="stylesheet"></noscript> <link href="/hugo-theme-relearn/css/perfect-scrollbar/perfect-scrollbar.min.css?1742828380" rel="stylesheet"> <link href="/hugo-theme-relearn/css/theme.min.css?1742828380" rel="stylesheet"> <link href="/hugo-theme-relearn/css/format-print.min.css?1742828380" rel="stylesheet" id="R-format-style"> <link href="/hugo-theme-relearn/css/auto-complete/auto-complete.min.css?1742828380" rel="stylesheet"> <script src="/hugo-theme-relearn/js/auto-complete/auto-complete.min.js?1742828380" defer></script> <script src="/hugo-theme-relearn/js/lunr/lunr.min.js?1742828380" defer></script> <script src="/hugo-theme-relearn/js/lunr/lunr.stemmer.support.min.js?1742828380" defer></script> <script src="/hugo-theme-relearn/js/lunr/lunr.multi.min.js?1742828380" defer></script> <script src="/hugo-theme-relearn/js/lunr/lunr.en.min.js?1742828380" defer></script> <script src="/hugo-theme-relearn/js/search.min.js?1742828380" defer></script> <script> window.relearn = window.relearn || {}; // configuration window.relearn.min = `.min`; window.relearn.path='\/configuration\/customization\/extending\/index.html'; window.relearn.relBasePath='..\/..\/..'; window.relearn.relBaseUri='..\/..\/..\/..'; window.relearn.absBaseUri='https:\/\/mcshelby.github.io\/hugo-theme-relearn'; window.relearn.contentLangs=['en']; window.relearn.index_js_url="/hugo-theme-relearn/searchindex.en.js?1742828380"; window.relearn.disableAnchorCopy=false; window.relearn.disableAnchorScrolling=false; window.relearn.disableInlineCopyToClipboard=true; window.relearn.enableBlockCodeWrap=true; // variant stuff window.relearn.themevariants = [ 'relearn-auto', 'relearn-dark', 'relearn-light', 'relearn-bright', 'zen-auto', 'zen-dark', 'zen-light', 'retro-auto', 'neon', 'learn', 'blue', 'green', 'red' ]; window.relearn.customvariantname = "my-custom-variant"; window.relearn.changeVariant = function(variant) { var oldVariant = document.documentElement.dataset.rThemeVariant; window.localStorage.setItem(window.relearn.absBaseUri + "/variant", variant); document.documentElement.dataset.rThemeVariant = variant; if (oldVariant != variant) { document.dispatchEvent( new CustomEvent('themeVariantLoaded', { detail: { variant, oldVariant } }) ); window.relearn.markVariant(); } } window.relearn.markVariant = function() { var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant"); document.querySelectorAll(".R-variantswitcher select").forEach((select) => {select.value = variant;}); } window.relearn.initVariant = function() { var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant") ?? ""; if( variant == window.relearn.customvariantname ){ }else if( !variant || !window.relearn.themevariants.includes(variant) ){ variant = window.relearn.themevariants[0]; window.localStorage.setItem(window.relearn.absBaseUri + "/variant", variant); } document.documentElement.dataset.rThemeVariant = variant; } window.relearn.initVariant(); window.relearn.markVariant(); // translations window.T_Copy_to_clipboard = `Copy to clipboard`; window.T_Copied_to_clipboard = `Copied to clipboard!`; window.T_Copy_link_to_clipboard = `Copy link to clipboard`; window.T_Link_copied_to_clipboard = `Copied link to clipboard!`; window.T_Reset_view = `Reset view`; window.T_View_reset = `View reset!`; window.T_No_results_found = `No results found for "{0}"`; window.T_N_results_found = `{1} results found for "{0}"`; </script> <script src="/hugo-theme-relearn/js/variant.min.js?1742828380"></script> <style> #R-body img.bg-white { background-color: white; } #R-logo { font-size: 1.875rem; margin-bottom: -.666rem; margin-top: -.666rem; max-width: 100%; width: 14.125rem; } @media only all and (max-width: 59.999rem) { #R-logo { font-size: 1.5625rem; margin-bottom: -.1875rem; margin-top: -.1875rem; } } #R-logo svg { display: inline-block; opacity: .945; vertical-align: middle; width: 29% !important; } @media only all and (max-width: 59.999rem) { #R-logo svg { width: 24.5% !important; } } #R-logo svg * { opacity: .945; } #R-logo .logo-title{ display: inline-block; text-align: left; text-wrap: wrap; overflow-wrap: break-word; vertical-align: middle; width: 4.5em; } </style> </head> <body class="mobile-support print" data-url="/hugo-theme-relearn/configuration/customization/extending/index.html"> <div id="R-body" class="default-animation"> <div id="R-body-overlay"></div> <nav id="R-topbar"> <div class="topbar-wrapper"> <div class="topbar-sidebar-divider"></div> <div class="topbar-area topbar-area-start" data-area="start"> <div class="topbar-button topbar-button-sidebar" data-content-empty="disable" data-width-s="show" data-width-m="hide" data-width-l="hide"><button class="topbar-control" onclick="toggleNav()" type="button" title="Menu (CTRL+ALT+n)"><i class="fa-fw fas fa-bars"></i></button> </div> <div class="topbar-button topbar-button-toc" data-content-empty="hide" data-width-s="show" data-width-m="show" data-width-l="show"><button class="topbar-control" onclick="toggleTopbarFlyout(this)" type="button" title="Table of Contents (CTRL+ALT+t)"><i class="fa-fw fas fa-list-alt"></i></button> <div class="topbar-content"> <div class="topbar-content-wrapper"> <nav class="TableOfContents"> <ul> <li><a href="#adding-javascript-or-stylesheets-to-all-pages">Adding JavaScript or Stylesheets to All Pages</a> <ul> <li><a href="#simple-solution">Simple Solution</a></li> <li><a href="#flexible-solution">Flexible Solution</a></li> </ul> </li> <li><a href="#custom-shortcodes-with-dependencies">Custom Shortcodes with Dependencies</a></li> </ul> </nav> </div> </div> </div> </div> <ol class="topbar-breadcrumbs breadcrumbs highlightable" itemscope itemtype="http://schema.org/BreadcrumbList"> <li itemscope itemtype="https://schema.org/ListItem" itemprop="itemListElement" class="a11y-only"><a itemprop="item" href="/hugo-theme-relearn/index.html"><span itemprop="name">Hugo Relearn Theme</span></a><meta itemprop="position" content="1"> > </li> <li itemscope itemtype="https://schema.org/ListItem" itemprop="itemListElement" class=""><a itemprop="item" href="/hugo-theme-relearn/configuration/index.html"><span itemprop="name">Configuration</span></a><meta itemprop="position" content="2"> > </li> <li itemscope itemtype="https://schema.org/ListItem" itemprop="itemListElement" class=""><a itemprop="item" href="/hugo-theme-relearn/configuration/customization/index.html"><span itemprop="name">Customization</span></a><meta itemprop="position" content="3"> > </li> <li itemscope itemtype="https://schema.org/ListItem" itemprop="itemListElement" class=""><span itemprop="name">Adding Scripts</span><meta itemprop="position" content="4"></li> </ol> <div class="topbar-area topbar-area-end" data-area="end"> <div class="topbar-button topbar-button-edit" data-content-empty="disable" data-width-s="area-more" data-width-m="show" data-width-l="show"><a class="topbar-control" href="https://github.com/McShelby/hugo-theme-relearn/edit/main/docs/content/configuration/customization/extending/_index.en.md" rel="external" target="_self" title="Edit (CTRL+ALT+w)"><i class="fa-fw fas fa-pen"></i></a> </div> <div class="topbar-button topbar-button-print" data-content-empty="disable" data-width-s="area-more" data-width-m="show" data-width-l="show"><a class="topbar-control" href="/hugo-theme-relearn/configuration/customization/extending/index.print.html" title="Print whole chapter (CTRL+ALT+p)"><i class="fa-fw fas fa-print"></i></a> </div> <div class="topbar-button topbar-button-prev" data-content-empty="disable" data-width-s="show" data-width-m="show" data-width-l="show"><a class="topbar-control" href="/hugo-theme-relearn/configuration/customization/partials/index.html" title="Partials (🡐)"><i class="fa-fw fas fa-chevron-left"></i></a> </div> <div class="topbar-button topbar-button-next" data-content-empty="disable" data-width-s="show" data-width-m="show" data-width-l="show"><a class="topbar-control" href="/hugo-theme-relearn/configuration/customization/linkeffects/index.html" title="Link Effects (🡒)"><i class="fa-fw fas fa-chevron-right"></i></a> </div> <div class="topbar-button topbar-button-more" data-content-empty="hide" data-width-s="show" data-width-m="show" data-width-l="show"><button class="topbar-control" onclick="toggleTopbarFlyout(this)" type="button" title="More"><i class="fa-fw fas fa-ellipsis-v"></i></button> <div class="topbar-content"> <div class="topbar-content-wrapper"> <div class="topbar-area topbar-area-more" data-area="more"> </div> </div> </div> </div> </div> </div> </nav> <div id="R-main-overlay"></div> <main id="R-body-inner" class="highlightable configuration" tabindex="-1"> <div class="flex-block-wrapper"> <article class="default"> <header class="headline"> </header> <h1 id="adding-scripts">Adding Scripts</h1> <p>A common question is how to add extra CSS styles or JavaScript to your site. This depends on what you need.</p> <h2 id="adding-javascript-or-stylesheets-to-all-pages">Adding JavaScript or Stylesheets to All Pages</h2> <h3 id="simple-solution">Simple Solution</h3> <p>Previous documentation of Hugo recommended adding <code>css/custom.css</code> and/or <code>js/custom.js</code> to your <code>static</code> or <code>assets</code> directory. This is supported by the theme.</p> <p>To gain more flexibility, see the <a href="/hugo-theme-relearn/configuration/customization/extending/index.html#flexible-solution">next section</a> below.</p> <h3 id="flexible-solution">Flexible Solution</h3> <p>To add CSS stylesheets, JavaScript files or any other addition to the <code><head></code> of every page, you can include them in <code>layouts/partials/custom-header.html</code> or <code>layouts/partials/custom-footer.html</code>.</p> <p>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.</p> <p>This way of customization will discard the mechanism for the <a href="/hugo-theme-relearn/configuration/customization/extending/index.html#simple-solution">simple solution</a> above. You will have to add code for inclusion of <code>css/custom.css</code> and/or <code>js/custom.js</code> yourself if you still need this.</p> <h2 id="custom-shortcodes-with-dependencies">Custom Shortcodes with Dependencies</h2> <p>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.</p> <p>For example, to create a shortcode called <code>myshortcode</code> that needs the <code>jquery</code> library:</p> <ol> <li> <p>Create the shortcode file <code>layouts/shortcodes/myshortcode.html</code> and add the folloging line somewhere:</p> <div class="tab-panel" data-tab-group="2b265df279578393fcc9cc7e59e93d0e"> <div class="tab-nav"> <div class="tab-nav-title">​</div> <button data-tab-item="layoutsshortcodesmyshortcodehtml" class="tab-nav-button tab-panel-style cstyle initial active" tabindex="-1" onclick="switchTab('2b265df279578393fcc9cc7e59e93d0e','layoutsshortcodesmyshortcodehtml')" > <span class="tab-nav-text">layouts/shortcodes/myshortcode.html</span> </button> </div> <div class="tab-content-container"> <div data-tab-item="layoutsshortcodesmyshortcodehtml" class="tab-content tab-panel-style cstyle initial active"> <div class="tab-content-text"> <div class="highlight wrap-code" dir="auto"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="o">...</span> </span></span><span class="line"><span class="cl"><span class="p">{{</span><span class="o">-</span> <span class="p">.</span><span class="nx">Page</span><span class="p">.</span><span class="nx">Store</span><span class="p">.</span><span class="nx">Set</span> <span class="s">"hasMyShortcode"</span> <span class="kc">true</span> <span class="p">}}</span> </span></span><span class="line"><span class="cl"><span class="o">...</span></span></span></code></pre></div> </div> </div> </div> </div></li> <li> <p><span class="badge cstyle cyan badge-with-title"><span class="badge-title"><i class="fa-fw fas fa-gears"></i></span><span class="badge-content">Option</span></span> Add this to your <code>hugo.toml</code>:</p> <div class="tab-panel" data-tab-group="config-code"> <div class="tab-nav"> <div class="tab-nav-title">hugo.</div> <button data-tab-item="toml" class="tab-nav-button tab-panel-style cstyle initial active" tabindex="-1" onclick="switchTab('config-code','toml')" > <span class="tab-nav-text">toml</span> </button> <button data-tab-item="yaml" class="tab-nav-button tab-panel-style cstyle initial" onclick="switchTab('config-code','yaml')" > <span class="tab-nav-text">yaml</span> </button> <button data-tab-item="json" class="tab-nav-button tab-panel-style cstyle initial" onclick="switchTab('config-code','json')" > <span class="tab-nav-text">json</span> </button> </div> <div class="tab-content-container"> <div data-tab-item="toml" class="tab-content tab-panel-style cstyle initial active"> <div class="tab-content-text"> <div class="highlight wrap-code" dir="auto"><pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="line"><span class="cl"><span class="p">[</span><span class="nx">params</span><span class="p">]</span> </span></span><span class="line"><span class="cl"> </span></span><span class="line"><span class="cl"><span class="p">[</span><span class="nx">relearn</span><span class="p">]</span> </span></span><span class="line"><span class="cl"> <span class="p">[</span><span class="nx">relearn</span><span class="p">.</span><span class="nx">dependencies</span><span class="p">]</span> </span></span><span class="line"><span class="cl"> <span class="p">[</span><span class="nx">relearn</span><span class="p">.</span><span class="nx">dependencies</span><span class="p">.</span><span class="nx">myshortcode</span><span class="p">]</span> </span></span><span class="line"><span class="cl"> <span class="nx">name</span> <span class="p">=</span> <span class="s1">'MyShortcode'</span></span></span></code></pre></div> </div> </div> <div data-tab-item="yaml" class="tab-content tab-panel-style cstyle initial"> <div class="tab-content-text"> <div class="highlight wrap-code" dir="auto"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">params</span><span class="p">:</span><span class="w"> </span>{}<span class="w"> </span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">relearn</span><span class="p">:</span><span class="w"> </span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">dependencies</span><span class="p">:</span><span class="w"> </span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">myshortcode</span><span class="p">:</span><span class="w"> </span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">MyShortcode</span></span></span></code></pre></div> </div> </div> <div data-tab-item="json" class="tab-content tab-panel-style cstyle initial"> <div class="tab-content-text"> <div class="highlight wrap-code" dir="auto"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">{</span> </span></span><span class="line"><span class="cl"> <span class="nt">"params"</span><span class="p">:</span> <span class="p">{},</span> </span></span><span class="line"><span class="cl"> <span class="nt">"relearn"</span><span class="p">:</span> <span class="p">{</span> </span></span><span class="line"><span class="cl"> <span class="nt">"dependencies"</span><span class="p">:</span> <span class="p">{</span> </span></span><span class="line"><span class="cl"> <span class="nt">"myshortcode"</span><span class="p">:</span> <span class="p">{</span> </span></span><span class="line"><span class="cl"> <span class="nt">"name"</span><span class="p">:</span> <span class="s2">"MyShortcode"</span> </span></span><span class="line"><span class="cl"> <span class="p">}</span> </span></span><span class="line"><span class="cl"> <span class="p">}</span> </span></span><span class="line"><span class="cl"> <span class="p">}</span> </span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre></div> </div> </div> </div> </div> </li> <li> <p>Create loader file <code>layouts/partials/dependencies/myshortcode.html</code>:</p> <div class="tab-panel" data-tab-group="8e35e7a7fe26547d2e385d5757d39680"> <div class="tab-nav"> <div class="tab-nav-title">​</div> <button data-tab-item="layoutspartialsdependenciesmyshortcodehtml" class="tab-nav-button tab-panel-style cstyle initial active" tabindex="-1" onclick="switchTab('8e35e7a7fe26547d2e385d5757d39680','layoutspartialsdependenciesmyshortcodehtml')" > <span class="tab-nav-text">layouts/partials/dependencies/myshortcode.html</span> </button> </div> <div class="tab-content-container"> <div data-tab-item="layoutspartialsdependenciesmyshortcodehtml" class="tab-content tab-panel-style cstyle initial active"> <div class="tab-content-text"> <div class="highlight wrap-code" dir="auto"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="p">{{</span><span class="o">-</span> <span class="k">if</span> <span class="nx">eq</span> <span class="p">.</span><span class="nx">location</span> <span class="s">"footer"</span> <span class="p">}}</span> </span></span><span class="line"><span class="cl"> <span class="p"><</span><span class="nx">script</span> <span class="nx">src</span><span class="p">=</span><span class="s">"https://www.unpkg.com/jquery/dist/jquery.js"</span><span class="p">><</span><span class="o">/</span><span class="nx">script</span><span class="p">></span> </span></span><span class="line"><span class="cl"><span class="p">{{</span><span class="o">-</span> <span class="nx">end</span> <span class="p">}}</span></span></span></code></pre></div> </div> </div> </div> </div></li> </ol> <p>Important notes:</p> <ul> <li>Character casing is relevant!</li> <li>The <code>name</code> in <code>hugo.toml</code> must match the <code>Store</code> key used in the shortcode file, prefixed with a <code>has</code>.</li> <li>The key of <code>relearn.dependencies</code> must match the loader file name.</li> </ul> <p>See the <code>math</code>, <code>mermaid</code>, and <code>openapi</code> shortcodes for examples.</p> <details open class=" box cstyle notices note"> <summary class="box-label" tabindex="-1"> <i class="fa-fw fas fa-exclamation-circle"></i> Note </summary> <div class="box-content"> <p>For advanced customization, you can use the dependency loader in your own partials:</p> <div class="highlight wrap-code" dir="auto"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="p">{{</span><span class="o">-</span> <span class="nx">partial</span> <span class="s">"dependencies.gotmpl"</span> <span class="p">(</span><span class="nx">dict</span> <span class="s">"page"</span> <span class="p">.</span> <span class="s">"location"</span> <span class="s">"mylocation"</span><span class="p">)</span> <span class="p">}}</span></span></span></code></pre></div> </div> </details> <p>Give a unique name for the <code>location</code> parameter when you call it, so you can distinguish your loaders behavior depending on the location it was called from.</p> <footer class="footline"> <div class="R-taxonomy taxonomy-categories cstyle" title="Categories" style="--VARIABLE-TAGS-BG-color: var(--INTERNAL-TAG-BG-color);"> <i class="fa-fw fas fa-layer-group"></i> <ul> <li><a class="term-link" href="/hugo-theme-relearn/categories/explanation/index.html">Explanation</a></li> <li><a class="term-link" href="/hugo-theme-relearn/categories/howto/index.html">Howto</a></li> </ul> </div> </footer> </article> </div> </main> </div> <script src="/hugo-theme-relearn/js/clipboard/clipboard.min.js?1742828380" defer></script> <script src="/hugo-theme-relearn/js/perfect-scrollbar/perfect-scrollbar.min.js?1742828380" defer></script> <script src="/hugo-theme-relearn/js/theme.min.js?1742828380" defer></script> </body> </html>