hugo-geekblog/exampleSite/content/posts/advanced/includes.md

101 lines
3.9 KiB
Markdown
Raw Normal View History

2021-11-23 13:33:03 +00:00
---
title: Includes
date: 2021-05-23T20:00:00+01:00
authors:
- john-doe
tags:
- Documentation
- Shortcodes
---
Include shortcode can include files of different types. By specifying a language, the included file will have syntax highlighting.
<!--more-->
{{< toc >}}
<!-- prettier-ignore-start -->
2021-11-23 13:33:03 +00:00
```tpl
{{</* include file="relative/path/from/hugo/root" language="go" */>}}
2021-11-23 13:33:03 +00:00
```
<!-- prettier-ignore-end -->
2021-11-23 13:33:03 +00:00
## Attributes
2021-11-23 13:33:03 +00:00
| Name | Description | default |
| -------- | ----------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
| file | path to the included file relative to the Hugo root | undefined |
| language | language for [syntax highlighting](https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages) | undefined |
| type | special include type (`html,page`) | undefined (rendered as markdown) |
| options | highlighting [options](https://gohugo.io/content-management/syntax-highlighting/#highlight-shortcode) | `linenos=table` |
2021-11-23 13:33:03 +00:00
## Usage
2021-11-23 13:33:03 +00:00
## Markdown file (default)
2021-11-23 13:33:03 +00:00
If no other options are specified, files will be rendered as Markdown using the `RenderString` [function](https://gohugo.io/functions/renderstring/).
{{< hint type=important >}}
2021-11-23 13:33:03 +00:00
**Location of markdown files**\
If you include markdown files that should not get a menu entry, place them outside the content folder or exclude them otherwise.
{{< /hint >}}
<!-- prettier-ignore -->
2021-11-23 13:33:03 +00:00
```tpl
{{</* include file="/static/_includes/example.md.part" */>}}
```
<!-- prettier-ignore-start -->
<!-- spellchecker-disable -->
{{< include file="/static/_includes/example.md.part" >}}
<!-- spellchecker-enable -->
<!-- prettier-ignore-end -->
## Language files
2021-11-23 13:33:03 +00:00
This method can be used to include source code files and keep them automatically up to date.
<!-- prettier-ignore -->
2021-11-23 13:33:03 +00:00
```tpl
{{</* include file="config.yaml" language="yaml" options="linenos=table,hl_lines=5-6,linenostart=100" */>}}
```
Result:
2021-11-23 13:33:03 +00:00
<!-- prettier-ignore-start -->
<!-- spellchecker-disable -->
{{< include file="config/_default/config.yaml" language="yaml" options="linenos=table,hl_lines=5-6,linenostart=100" >}}
2021-11-23 13:33:03 +00:00
<!-- spellchecker-enable -->
<!-- prettier-ignore-end -->
## Special include types
2021-11-23 13:33:03 +00:00
### HTML
2021-11-23 13:33:03 +00:00
HTML content will be filtered by the `safeHTML` filter and added to the rendered page output.
<!-- prettier-ignore -->
2021-11-23 13:33:03 +00:00
```tpl
{{</* include file="/static/_includes/example.html.part" type="html" */>}}
2021-11-23 13:33:03 +00:00
```
{{< include file="/static/_includes/example.html.part" type="html" >}}
### Pages
2021-11-23 13:33:03 +00:00
In some situations, it can be helpful to include Markdown files that also contain shortcodes. While the [default method](#markdown-file-default) works fine to render plain Markdown, shortcodes are not parsed. The only way to get this to work is to use Hugo pages. There are several ways to structure these include pages, so whatever you do, keep in mind that Hugo needs to be able to render and serve these files as regular pages! How it works:
1. First you need to create a directory **within** your content directory. For this example site `_includes` is used.
2. Place your Markdown files within the `_includes` folder e.g. `/_includes/include-page.md`. Make sure to name it `*.md`.
2022-01-10 22:39:30 +00:00
3. Include the page using `{{</* include file="/_includes/include-page.md" type="page" */>}}`.
2021-11-23 13:33:03 +00:00
Resulting structure should look like this:
```shell
2021-11-23 13:33:03 +00:00
_includes/
├── include-page.md
└── _index.md
```
{{< include file="/_includes/include-page.md" type="page" >}}