Skip to content

Commit 29e1dec

Browse files
aimeeugeriom
authored andcommitted
add suggestions; update userguide
1 parent 79e44b8 commit 29e1dec

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

layouts/shortcodes/readfile.html

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
{{ end }}
2525

2626

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

3030
{{ if fileExists ($.Scratch.Get "filepath") }}
3131
{{ if eq (.Get "code") "true" }}
@@ -34,4 +34,8 @@
3434
{{ else }}
3535
{{- $.Scratch.Get "filepath" | os.ReadFile | .Page.RenderString | safeHTML -}}
3636
{{ end }}
37-
{{ else }}{{- errorf "File not found: %s %s" ($.Scratch.Get "filepath") .Position -}}{{ end }}
37+
{{ else if eq (.Get "draft") "true" }}
38+
39+
<p style="color: #D74848"><b><i>The file <code>{{ $.Scratch.Get "filepath" }}</code> was not found.</i></b></p>
40+
41+
{{ else }}{{- errorf "Shortcode %q: file %q not found at %s" .Name ($.Scratch.Get "filepath") .Position -}}{{ end }}

userguide/content/en/docs/adding-content/shortcodes/index.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -679,14 +679,28 @@ under the parent file's working directory are supported.
679679
For files outside the current working directory you can use an absolute path
680680
starting with `/`. The root directory is the `/content` folder.
681681

682-
683-
684682
| Parameter | Default | Description |
685683
| ---------------- |------------| ------------|
686684
| file | | Path of external file|
687685
| code | false | Boolean value. If `true` the contents is treated as code|
688686
| lang | plain text | Programming language |
689687

688+
### Error reporting
689+
690+
If the shortcode can't find the specified file, the shortcode throws a compile error.
691+
692+
In the following example, Hugo throws a compile error if it can't find `includes/deploy.yml`:
693+
694+
```go-html-template
695+
{{</* readfile file="includes/deploy.yaml" code="true" lang="yaml" */>}}
696+
```
697+
698+
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:
699+
700+
```go-html-template
701+
{{</* readfile file="includes/deploy.yaml" code="true" lang="yaml" draft="true" */>}}
702+
```
703+
690704
## Conditional text
691705

692706
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.

0 commit comments

Comments
 (0)