|
| 1 | +--- |
| 2 | +title: Basic structure |
| 3 | +--- |
| 4 | + |
| 5 | +::: {.pale-blue} |
| 6 | + |
| 7 | +**On this page we will:** |
| 8 | + |
| 9 | +* Understand the two core files in a basic Quarto website. |
| 10 | +* Learn how to render and preview the website. |
| 11 | + |
| 12 | +::: |
| 13 | + |
| 14 | +<br> |
| 15 | + |
| 16 | +We'll be creating a **Quarto website**. A simple website project just needs two key files: |
| 17 | + |
| 18 | +1. `_quarto.yml` |
| 19 | +2. `index.qmd` |
| 20 | + |
| 21 | +## `_quarto.yml` |
| 22 | + |
| 23 | +This is file defines your **project settings** and **website structure**. |
| 24 | + |
| 25 | +The file uses YAML, which is just a plain text format for settings, written as **key: value** pairs, for example `title: My website`. Indentation (spaces at the start of a line) shows that something belongs "inside" something else, a bit like folders within folders. |
| 26 | + |
| 27 | +Below is an example from [quarto-template](https://github.com/pythonhealthdatascience/quarto-template){target="_blank"}. Hover over each line to see an explanation: |
| 28 | + |
| 29 | +```yaml |
| 30 | +project: # <1> |
| 31 | + type: website # <1> |
| 32 | + |
| 33 | +website: # <1> |
| 34 | + title: "Quarto Template" # <2> |
| 35 | + navbar: # <3> |
| 36 | + right: # <4> |
| 37 | + - icon: github # <4> |
| 38 | + text: GitHub # <4> |
| 39 | + href: https://github.com/pythonhealthdatascience/quarto-template/ # <4> |
| 40 | + sidebar: # <5> |
| 41 | + - logo: images/exeter.png # <6> |
| 42 | + contents: # <7> |
| 43 | + - text: Overview # <7> |
| 44 | + href: index.qmd # <7> |
| 45 | + - pages/1_making_a_change.qmd # <7> |
| 46 | + - pages/2_markdown_basics.qmd # <7> |
| 47 | + - pages/3_quarto_features.qmd # <7> |
| 48 | + - pages/4_images_videos_documents.qmd # <7> |
| 49 | + - pages/5_inline_html.qmd # <7> |
| 50 | + |
| 51 | +format: # <8> |
| 52 | + html: # <8> |
| 53 | + theme: default # <8> |
| 54 | +``` |
| 55 | +
|
| 56 | +1. **Website:** Defines that this project builds as a website. |
| 57 | +2. **Website title:** Appears in broswer tab and navbar. |
| 58 | +3. **Navbar:** The navigation bar across the top of your site. |
| 59 | +4. **GitHub link:** Adds a GitHub icon on the right side of the navbar linking to the repository (which contains the source files for the site). |
| 60 | +5. **Sidebar:** Displays navigation links down the left-hand side. |
| 61 | +6. **Logo:** The image shown at the top of the sidebar. |
| 62 | +7. **Sidebar contents:** Lists website pages. Will list using page titles by default, but can override with `text` (title to use) and `href` (path to file). |
| 63 | +8. **Theme:** Site theme (covered on [Customising the site appearance](/pages/customising/index.qmd) page). |
| 64 | + |
| 65 | +::: {.callout-note title="Optional extra: books" collapse="true"} |
| 66 | + |
| 67 | +A **Quarto book** is basically a website with chapter-style navigation. Under the hood, it's still a Quarto website - just organised a little differently. |
| 68 | + |
| 69 | +See the [books guide](https://quarto.org/docs/books/book-output.html) for more information. |
| 70 | + |
| 71 | +Example: |
| 72 | + |
| 73 | +```yaml |
| 74 | +project: |
| 75 | + type: book |
| 76 | +
|
| 77 | +book: |
| 78 | + title: "Quarto Template" |
| 79 | + favicon: images/exeter.png |
| 80 | + chapters: |
| 81 | + - text: Overview |
| 82 | + href: index.qmd |
| 83 | + - pages/TODO.qmd |
| 84 | + navbar: |
| 85 | + right: |
| 86 | + - icon: github |
| 87 | + text: GitHub |
| 88 | + href: https://github.com/pythonhealthdatascience/quarto-template/ |
| 89 | + sidebar: |
| 90 | + logo: images/exeter.png |
| 91 | +``` |
| 92 | + |
| 93 | +::: |
| 94 | + |
| 95 | +## `index.qmd` |
| 96 | + |
| 97 | +A `.qmd` file is a **Quarto Markdown** document. It mixes metadata at the top, text, and (optionally) code. For a website, `index.qmd` is treated as the **homepage:** when someone visits the root of your site, Quarto serves `index.html`, which comes from `index.qmd. |
| 98 | + |
| 99 | +> If you're familiar with R Markdown (`.Rmd`), these are similar, but `.qmd` is more flexible. |
| 100 | + |
| 101 | +Each `.qmd` file begins with **YAML front matter**, enclosed by three dashes (`---`). This defines metadata such as the page title. Below that is the page. Here we just have one sentence: `This is my homepage.`. |
| 102 | + |
| 103 | +```{.r} |
| 104 | +--- |
| 105 | +title: My homepage |
| 106 | +--- |
| 107 | +
|
| 108 | +
|
| 109 | +This is my homepage. |
| 110 | +``` |
| 111 | + |
| 112 | +## Rendering your website |
| 113 | + |
| 114 | +**Rendering** means converting your Quarto source files into the final website - a collection of `.html` pages that live inside a folder called `_site`. To render your site: |
| 115 | + |
| 116 | +1. Open `index.qmd` in the editor, and click the **Preview** button in the top right hand corner |
| 117 | + |
| 118 | +{fig-alt="Screenshot of VSCode with Preview button circled."} |
| 119 | + |
| 120 | +2. In the bottom panel in the Terminal, you should see it says **Preparing to preview** and runs through files. It is creating `_sites/` folder containing your rendered site. Then when it is done, green text saying `Browse at ...`. |
| 121 | + |
| 122 | +{fig-alt="Screenshot of the 'Preparing to preview' message in the Terminal."} |
| 123 | + |
| 124 | +3. You are now viewing a **live preview** of your site in the browser. Nothing is public yet; this preview only runs on your own computer. In other words, you are the only person who can see this site right now, and it will not appear on the internet until you deliberately publish or upload the rendered files somewhere. |
| 125 | + |
| 126 | + By default, VSCode will open the preview in-line. This will be quite a small preview - to open it in a new tab, either copy the URL from the Terminal (`http://localhost:...`) or click the "Open in Browser" button in the top right. |
| 127 | + |
| 128 | +{fig-alt="Screenshot of preview opened in-line, with cursor hovering over the 'Open in Browser' button."} |
| 129 | + |
| 130 | +<br> |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +::: {.callout-note title="Optional extra: rendering via the terminal" collapse="true"} |
| 135 | + |
| 136 | +Alternatively, you can render and preview your site directly via commands in the terminal. |
| 137 | + |
| 138 | +In the bottom panel, ensure you are on the **Terminal** tab. The Terminal is a place where you type commands instead of clicking buttons. We just need to type `quarto preview` then press <kbd>Enter</kbd> on your keyboard. |
| 139 | + |
| 140 | +{fig-alt="Screenshot of VSCode with 'quarto preview' command in Terminal circled."} |
| 141 | + |
| 142 | +You can then use the `http://localhost:...` link printed in the Terminal to open your site in the browser. |
| 143 | + |
| 144 | +With this method, the site updates automatically whenever files are saved. In Codespaces, files are auto‑saved very frequently, so the preview will re-render often; for that reason, the **Preview** button described above is usually a calmer option during this workshop. |
| 145 | + |
| 146 | +::: |
0 commit comments