|
| 1 | += Hugo Bookshop |
| 2 | +ifdef::env-github[] |
| 3 | +:tip-caption: :bulb: |
| 4 | +:note-caption: :information_source: |
| 5 | +:important-caption: :heavy_exclamation_mark: |
| 6 | +:caution-caption: :fire: |
| 7 | +:warning-caption: :warning: |
| 8 | +endif::[] |
| 9 | +:toc: |
| 10 | +:toc-placement!: |
| 11 | + |
| 12 | +toc::[] |
| 13 | + |
| 14 | +IMPORTANT: Hugo support should be treated as a semi-stable beta |
| 15 | + |
| 16 | +== Quick start |
| 17 | +To jump right into using Bookshop on a Hugo site, check out the link:https://github.com/CloudCannon/hugo-bookshop-starter[Hugo Bookshop Starter] |
| 18 | + |
| 19 | +== Hugo Configuration |
| 20 | + |
| 21 | +To use Bookshop with Hugo, the primary dependency is the `bookshop/hugo` module. This provides the bookshop partials needed to render components on your website. This import should be placed in your **site** config.toml. |
| 22 | + |
| 23 | +.*site/config.toml* |
| 24 | +```toml |
| 25 | +[[module.imports]] |
| 26 | +path = 'github.com/cloudcannon/bookshop/hugo/v2' |
| 27 | +``` |
| 28 | + |
| 29 | +The best workflow is then to create a new Hugo module to house your components. This can then be used across multiple websites using the Hugo module system. |
| 30 | + |
| 31 | +.*bash* |
| 32 | +```bash |
| 33 | +hugo mod init github.com/example/components |
| 34 | +``` |
| 35 | + |
| 36 | +The file structure within this components module is described in the link:conventions.adoc[Conventions Guide] |
| 37 | + |
| 38 | +The last piece of config is adding the following mounts to the `config.toml` in your **components** module. |
| 39 | + |
| 40 | +.*components/config.toml* |
| 41 | +```toml |
| 42 | +[[module.mounts]] |
| 43 | +source = "." |
| 44 | +target = "layouts/partials/bookshop" |
| 45 | + |
| 46 | +[[module.mounts]] |
| 47 | +source = "." |
| 48 | +target = "assets/bookshop" |
| 49 | +``` |
| 50 | + |
| 51 | +These mounts allow the `bookshop/hugo` module to locate your components. |
| 52 | + |
| 53 | +See the link:https://github.com/CloudCannon/hugo-bookshop-starter[Hugo Bookshop Starter] for an example of how all of the above looks in practice. |
| 54 | + |
| 55 | +== Writing components |
| 56 | + |
| 57 | +Let's look at an example `button.hugo.html` file. |
| 58 | +``` |
| 59 | +components/ |
| 60 | +└─ button/ |
| 61 | + └─ button.hugo.html |
| 62 | +``` |
| 63 | +These files are namespaced for the static site generator when the filetype is ambiguous, which is why the file is `button.hugo.html` and not `button.html`. Beyond the naming convention, these files are what you would expect when working with Hugo. Our `button.hugo.html` file might look like: |
| 64 | +```go |
| 65 | +<a class="c-button" href="{{ .link_url }}">{{ .text }}</a> |
| 66 | +``` |
| 67 | +This looks like a normal Hugo include because... it is. While Bookshop provides developer tooling, the job of the `bookshop/hugo` module is to tell Hugo where to find component files. Loading and parsing these files goes through the normal Hugo partial flow. |
| 68 | + |
| 69 | +TIP: The `@bookshop/init` package helps create component structures for you. Running `npx @bookshop/init --component button` would create the button structure needed for Hugo. |
| 70 | + |
| 71 | +== Using components |
| 72 | + |
| 73 | +To use components directly in a template, use the `bookshop` partial with the `components` syntax. |
| 74 | + |
| 75 | +.*index.html* |
| 76 | +```html |
| 77 | +... |
| 78 | +<div class="hero"> |
| 79 | + {{ partial "bookshop" (dict "component" "hero" "title" .Params.title "image" .Params.image) }} |
| 80 | + {{ partial "bookshop" (dict "component" "button" "label" .Params.cta_text "link_url" .Params.cta_url) }} |
| 81 | +</div> |
| 82 | +... |
| 83 | +``` |
| 84 | + |
| 85 | +This tag expects a dict containing a `component` field that references the Bookshop key of a component, as well as any fields to pass to the partial. |
| 86 | + |
| 87 | +To render a list of components, you can pass a structure or an array of structures to the `bookshop` partial: |
| 88 | + |
| 89 | +.*index.html* |
| 90 | +```html |
| 91 | +--- |
| 92 | +components: |
| 93 | + - _bookshop_name: hero |
| 94 | + title: "Hello World" |
| 95 | + image: /image.png |
| 96 | + - _bookshop_name: button |
| 97 | + cta_text: "Get Started" |
| 98 | + link_url: / |
| 99 | +--- |
| 100 | +<div class="hero"> |
| 101 | + {{ partial "bookshop" .Params.components }} |
| 102 | +</div> |
| 103 | +``` |
| 104 | + |
| 105 | +This tag expects either an object containing a `_bookshop_name` key, or an array of objects containing `_bookshop_name` keys. |
| 106 | + |
| 107 | +TIP: _The structures generated by Bookshop for CloudCannon include the `_bookshop_name` field for you. If you're not using structures you will need to add the `_bookshop_name` key by hand_ |
| 108 | + |
| 109 | +== Using Bookshop Partials |
| 110 | + |
| 111 | +Bookshop partials can be placed in the `shared/hugo` directory. i.e: |
| 112 | +```text |
| 113 | +component-library/ |
| 114 | +├─ components/ |
| 115 | +└─ shared/ |
| 116 | + └─ hugo/ |
| 117 | + └─ helper.hugo.html |
| 118 | +``` |
| 119 | + |
| 120 | +This can then be included using the `bookshop` partial with the `partial` syntax: |
| 121 | +```html |
| 122 | + {{ partial "bookshop" (dict "partial" "helper" "lorem" "ipsum") }} |
| 123 | +``` |
| 124 | + |
| 125 | +This tag expects a dict containing a `partial` field that references the Bookshop key of a partial, as well as any fields to pass to the partial. |
| 126 | + |
| 127 | +This is otherwise a standard Hugo partial, with the extra feature that it can be used anywhere within your Hugo site _or_ your components. |
| 128 | + |
| 129 | +== Importing styles |
| 130 | + |
| 131 | +To import Bookshop styles in Hugo, the plugin provides a `bookshop_scss` partial, which returns a slice of all SCSS resources in your bookshop. This can then be used as such: |
| 132 | + |
| 133 | +.*baseof.html* |
| 134 | +```html |
| 135 | +{{ $bookshop_scss_files := partial "bookshop_scss" . }} |
| 136 | +{{ $scss := $bookshop_scss_files | resources.Concat "css/bookshop.css" | resources.ToCSS | resources.Minify | resources.Fingerprint }} |
| 137 | +<link rel="stylesheet" href="{{ $scss.Permalink }}"> |
| 138 | +``` |
0 commit comments