feat: add env shortcode for conditional content rendering based on Hugo environment

This commit is contained in:
Cell
2025-12-22 13:59:47 +08:00
parent 4cd9a6940e
commit 9bcdbe3325
3 changed files with 25 additions and 6 deletions
+3 -5
View File
@@ -194,8 +194,6 @@ function isObjectLiteral(str) {
## Gist Embed
```md
{{</* gist Lruihao fb8b2d0353465c4d40bf74818db80710 */>}}
```
<!-- {{< gist Lruihao fb8b2d0353465c4d40bf74818db80710 >}} -->
{{< env "production" >}}
{{< gist Lruihao fb8b2d0353465c4d40bf74818db80710 >}}
{{< /env >}}
+1 -1
View File
@@ -1,4 +1,4 @@
{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251222034819-3fa866fa" -}}
{{- hugo.Store.Set "version" "v0.4.0-alpha.3-20251222065334-6cf32775" -}}
{{- .Store.Set "this" dict -}}
{{- partial "init/detection-env.html" . -}}
+21
View File
@@ -0,0 +1,21 @@
{{- /*
A shortcode to conditionally render content based on the current Hugo environment.
Hugo environments (development, production, etc.) usage reference:
- https://gohugo.io/functions/hugo/environment/
- https://gohugo.io/configuration/all/#environment
For example, to render content only in the "production" environment:
{{< env "production" >}}
This content is only visible in production.
{{< /env >}}
*/ -}}
{{- $env := .Get 0 -}}
{{- if eq $env hugo.Environment -}}
{{- .Inner | .Page.RenderString -}}
{{- else if hugo.IsServer -}}
<details>
<summary>[Hugo Server]: Click to expand <strong><code>{{ $env }}</code></strong> environment content.</summary>
<pre><code>{{- printf `%s` "\n" | trim .Inner -}}</code></pre>
</details>
{{- end -}}