mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
@@ -1,41 +0,0 @@
|
||||
---
|
||||
title: os.FileExists
|
||||
description: Reports whether the file or directory exists.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [fileExists]
|
||||
related:
|
||||
- functions/os/Getenv
|
||||
- functions/os/ReadDir
|
||||
- functions/os/ReadFile
|
||||
- functions/os/Stat
|
||||
returnType: bool
|
||||
signatures: [os.FileExists PATH]
|
||||
aliases: [/functions/fileexists]
|
||||
---
|
||||
|
||||
The `os.FileExists` function attempts to resolve the path relative to the root of your project directory. If a matching file or directory is not found, it will attempt to resolve the path relative to the [`contentDir`](/getting-started/configuration#contentdir). A leading path separator (`/`) is optional.
|
||||
|
||||
With this directory structure:
|
||||
|
||||
```text
|
||||
content/
|
||||
├── about.md
|
||||
├── contact.md
|
||||
└── news/
|
||||
├── article-1.md
|
||||
└── article-2.md
|
||||
```
|
||||
|
||||
The function returns these values:
|
||||
|
||||
```go-html-template
|
||||
{{ fileExists "content" }} → true
|
||||
{{ fileExists "content/news" }} → true
|
||||
{{ fileExists "content/news/article-1" }} → false
|
||||
{{ fileExists "content/news/article-1.md" }} → true
|
||||
{{ fileExists "news" }} → true
|
||||
{{ fileExists "news/article-1" }} → false
|
||||
{{ fileExists "news/article-1.md" }} → true
|
||||
```
|
@@ -1,61 +0,0 @@
|
||||
---
|
||||
title: os.Getenv
|
||||
description: Returns the value of an environment variable, or an empty string if the environment variable is not set.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [getenv]
|
||||
related:
|
||||
- functions/os/FileExists
|
||||
- functions/os/ReadDir
|
||||
- functions/os/ReadFile
|
||||
- functions/os/Stat
|
||||
returnType: string
|
||||
signatures: [os.Getenv VARIABLE]
|
||||
aliases: [/functions/getenv]
|
||||
toc: true
|
||||
---
|
||||
|
||||
## Security
|
||||
|
||||
By default, when using the `os.Getenv` function Hugo allows access to:
|
||||
|
||||
- The `CI` environment variable
|
||||
- Any environment variable beginning with `HUGO_`
|
||||
|
||||
To access other environment variables, adjust your site configuration. For example, to allow access to the `HOME` and `USER` environment variables:
|
||||
|
||||
{{< code-toggle file=hugo >}}
|
||||
[security.funcs]
|
||||
getenv = ['^HUGO_', '^CI$', '^USER$', '^HOME$']
|
||||
{{< /code-toggle >}}
|
||||
|
||||
Read more about Hugo's [security policy].
|
||||
|
||||
[security policy]: /about/security-model/#security-policy
|
||||
|
||||
## Examples
|
||||
|
||||
```go-html-template
|
||||
{{ getenv "HOME" }} → /home/victor
|
||||
{{ getenv "USER" }} → victor
|
||||
```
|
||||
|
||||
You can pass values when building your site:
|
||||
|
||||
```sh
|
||||
MY_VAR1=foo MY_VAR2=bar hugo
|
||||
|
||||
OR
|
||||
|
||||
export MY_VAR1=foo
|
||||
export MY_VAR2=bar
|
||||
hugo
|
||||
```
|
||||
|
||||
And then retrieve the values within a template:
|
||||
|
||||
```go-html-template
|
||||
{{ getenv "MY_VAR1" }} → foo
|
||||
{{ getenv "MY_VAR2" }} → bar
|
||||
```
|
@@ -1,51 +0,0 @@
|
||||
---
|
||||
title: os.ReadDir
|
||||
description: Returns an array of FileInfo structures sorted by file name, one element for each directory entry.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [readDir]
|
||||
related:
|
||||
- functions/os/FileExists
|
||||
- functions/os/Getenv
|
||||
- functions/os/ReadFile
|
||||
- functions/os/Stat
|
||||
returnType: os.FileInfo
|
||||
signatures: [os.ReadDir PATH]
|
||||
aliases: [/functions/readdir]
|
||||
---
|
||||
|
||||
The `os.ReadDir` function resolves the path relative to the root of your project directory. A leading path separator (`/`) is optional.
|
||||
|
||||
With this directory structure:
|
||||
|
||||
```text
|
||||
content/
|
||||
├── about.md
|
||||
├── contact.md
|
||||
└── news/
|
||||
├── article-1.md
|
||||
└── article-2.md
|
||||
```
|
||||
|
||||
This template code:
|
||||
|
||||
```go-html-template
|
||||
{{ range readDir "content" }}
|
||||
{{ .Name }} → {{ .IsDir }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
Produces:
|
||||
|
||||
```html
|
||||
about.md → false
|
||||
contact.md → false
|
||||
news → true
|
||||
```
|
||||
|
||||
Note that `os.ReadDir` is not recursive.
|
||||
|
||||
Details of the `FileInfo` structure are available in the [Go documentation](https://pkg.go.dev/io/fs#FileInfo).
|
||||
|
||||
For more information on using `readDir` and `readFile` in your templates, see [Local File Templates](/templates/files).
|
@@ -1,40 +0,0 @@
|
||||
---
|
||||
title: os.ReadFile
|
||||
description: Returns the contents of a file.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [readFile]
|
||||
related:
|
||||
- functions/os/FileExists
|
||||
- functions/os/Getenv
|
||||
- functions/os/ReadDir
|
||||
- functions/os/Stat
|
||||
returnType: string
|
||||
signatures: [os.ReadFile PATH]
|
||||
aliases: [/functions/readfile]
|
||||
---
|
||||
|
||||
The `os.ReadFile` function attempts to resolve the path relative to the root of your project directory. If a matching file is not found, it will attempt to resolve the path relative to the [`contentDir`](/getting-started/configuration#contentdir). A leading path separator (`/`) is optional.
|
||||
|
||||
With a file named README.md in the root of your project directory:
|
||||
|
||||
```text
|
||||
This is **bold** text.
|
||||
```
|
||||
|
||||
This template code:
|
||||
|
||||
```go-html-template
|
||||
{{ readFile "README.md" }}
|
||||
```
|
||||
|
||||
Produces:
|
||||
|
||||
```html
|
||||
This is **bold** text.
|
||||
```
|
||||
|
||||
Note that `os.ReadFile` returns raw (uninterpreted) content.
|
||||
|
||||
For more information on using `readDir` and `readFile` in your templates, see [Local File Templates](/templates/files).
|
@@ -1,31 +0,0 @@
|
||||
---
|
||||
title: os.Stat
|
||||
description: Returns a FileInfo structure describing a file or directory.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/os/FileExists
|
||||
- functions/os/Getenv
|
||||
- functions/os/ReadDir
|
||||
- functions/os/ReadFile
|
||||
returnType: os.FileInfo
|
||||
signatures: [os.Stat PATH]
|
||||
aliases: [/functions/os.stat]
|
||||
---
|
||||
|
||||
The `os.Stat` function attempts to resolve the path relative to the root of your project directory. If a matching file or directory is not found, it will attempt to resolve the path relative to the [`contentDir`](/getting-started/configuration#contentdir). A leading path separator (`/`) is optional.
|
||||
|
||||
```go-html-template
|
||||
{{ $f := os.Stat "README.md" }}
|
||||
{{ $f.IsDir }} → false (bool)
|
||||
{{ $f.ModTime }} → 2021-11-25 10:06:49.315429236 -0800 PST (time.Time)
|
||||
{{ $f.Name }} → README.md (string)
|
||||
{{ $f.Size }} → 241 (int64)
|
||||
|
||||
{{ $d := os.Stat "content" }}
|
||||
{{ $d.IsDir }} → true (bool)
|
||||
```
|
||||
|
||||
Details of the `FileInfo` structure are available in the [Go documentation](https://pkg.go.dev/io/fs#FileInfo).
|
@@ -1,12 +0,0 @@
|
||||
---
|
||||
title: OS functions
|
||||
linkTitle: os
|
||||
description: Template functions to interact with the operating system.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
---
|
||||
|
||||
Use these functions to interact with the operating system.
|
Reference in New Issue
Block a user