Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcodes: Update readfile to throw compile error when file not found #1457

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions layouts/shortcodes/readfile.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
{{ end }}


{{/* If the file exists, read it and highlight it if it's code. Throw an error
if the file is not found */}}
{{/* If the file exists, read it and highlight it if it's code.
Throw a compile error or print an error on the page if the file is not found */}}

{{ if fileExists ($.Scratch.Get "filepath") }}
{{ if eq (.Get "code") "true" }}
Expand All @@ -34,8 +34,8 @@
{{ else }}
{{- $.Scratch.Get "filepath" | os.ReadFile | .Page.RenderString | safeHTML -}}
{{ end }}
{{ else }}
{{ else if eq (.Get "draft") "true" }}

<p style="color: #D74848"><b><i>The file <code>{{ $.Scratch.Get "filepath" }}</code> was not found.</i></b></p>
<p style="color: #D74848"><b><i>The file <code>{{ $.Scratch.Get "filepath" }}</code> was not found.</i></b></p>

{{ end }}
{{ else }}{{- errorf "Shortcode %q: file %q not found at %s" .Name ($.Scratch.Get "filepath") .Position -}}{{ end }}
18 changes: 16 additions & 2 deletions userguide/content/en/docs/adding-content/shortcodes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,14 +679,28 @@ under the parent file's working directory are supported.
For files outside the current working directory you can use an absolute path
starting with `/`. The root directory is the `/content` folder.



| Parameter | Default | Description |
| ---------------- |------------| ------------|
| file | | Path of external file|
| code | false | Boolean value. If `true` the contents is treated as code|
| lang | plain text | Programming language |

### Error reporting

If the shortcode can't find the specified file, the shortcode throws a compile error.

In the following example, Hugo throws a compile error if it can't find `includes/deploy.yml`:

```go-html-template
{{</* readfile file="includes/deploy.yaml" code="true" lang="yaml" */>}}
```

Alternately, Hugo you can display a message on the rendered page instead of throwing a compile error. Add `draft="true"` as a parameter. For example:

```go-html-template
{{</* readfile file="includes/deploy.yaml" code="true" lang="yaml" draft="true" */>}}
```

## Conditional text

The `conditional-text` shortcode allows you to show or hide parts of your content depending on the value of the `buildCondition` parameter set in your configuration file. This can be useful if you are generating different builds from the same source, for example, using a different product name. This shortcode helps you handle the minor differences between these builds.
Expand Down