### Local Development Setup Source: https://github.com/gohugoio/gotmplfmt/blob/main/vscode/README.md Steps to build and install the extension locally for development. This includes installing the CLI, installing npm dependencies, compiling the extension, and installing the VSIX package. ```bash # From the repo root, install the CLI: go install . # Build and install the extension: cd vscode npm install npm run compile code --install-extension gotmplfmt-*.vsix || npx vsce package && code --install-extension gotmplfmt-*.vsix ``` -------------------------------- ### Install gotmplfmt CLI Source: https://github.com/gohugoio/gotmplfmt/blob/main/vscode/README.md Install the gotmplfmt CLI using Go. Ensure Go is installed and on your PATH. ```bash go install github.com/gohugoio/gotmplfmt@latest ``` -------------------------------- ### Install gotmplfmt CLI and VS Code extension Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Instructions for installing the gotmplfmt CLI binary and the corresponding VS Code extension. ```bash # From the command line code --install-extension gohugoio.gotmplfmt # Or install the CLI (required by the extension): go install github.com/gohugoio/gotmplfmt@latest # macOS via Homebrew: brew install --cask gohugoio/tap/gotmplfmt ``` -------------------------------- ### GitHub Actions CI Formatting Check Source: https://github.com/gohugoio/gotmplfmt/blob/main/README.md Example steps for a GitHub Actions workflow to install gotmplfmt and check Go template formatting. ```yaml steps: - name: Install gotmplfmt run: go install github.com/gohugoio/gotmplfmt@latest - name: Check go template formatting run: "diff <(gotmplfmt -d layouts) <(printf '')" ``` -------------------------------- ### Example CSS Content Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/directory.txt Sample CSS content, which should be skipped by gotmplfmt. ```css body { color: red; } ``` -------------------------------- ### Install gotmplfmt via Homebrew Source: https://github.com/gohugoio/gotmplfmt/blob/main/vscode/README.md Install the gotmplfmt CLI on macOS using Homebrew. ```bash brew install --cask gohugoio/tap/gotmplfmt ``` -------------------------------- ### Example HTML Content Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/directory.txt Sample HTML content that might be processed by gotmplfmt. ```html
hello
``` -------------------------------- ### Example XML Content Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/directory.txt Sample XML content that might be processed by gotmplfmt. ```xml ``` -------------------------------- ### Hugo 'with...else' Example Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/in/with-else.html This snippet shows how to use the 'with...else' control structure. It first checks if $v1 is truthy. If not, it checks if $v2 is truthy. If both are falsy, it prints a message. ```gohtml {{ $v1 := 0 }} {{ $v2 := 42 }} {{ with $v1 }} {{ . }} {{ else with $v2 }} {{ . }} {{ else }} {{ print "v1 and v2 are falsy" }} {{ end }} ``` -------------------------------- ### Version Information (CLI) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Print the current version, VCS commit hash, and build date of the gotmplfmt tool. ```bash gotmplfmt -version # Output: gotmplfmt v0.4.0 (commit: abc1234, date: 2024-10-01T12:00:00Z) ``` -------------------------------- ### CLI: Version information (-version) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Prints the current version, VCS commit hash, and build date. ```APIDOC ## CLI: Version information (-version) Prints the current version, VCS commit hash, and build date. ### Usage ```bash gotmplfmt -version ``` ### Example ```bash gotmplfmt -version # Output: gotmplfmt v0.4.0 (commit: abc1234, date: 2024-10-01T12:00:00Z) ``` ``` -------------------------------- ### Format and Write Back to Source File Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/write-flag.txt Use the -w flag to format the input.html file and write the changes directly back to it. Verify the changes by comparing the modified input.html with expected.html. Standard output should be empty. ```bash gotmplfmt -w input.html cmp input.html expected.html ``` -------------------------------- ### Directory Walking (CLI) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt When given a directory path, gotmplfmt recursively walks all subdirectories and processes every supported template file. Non-template files are skipped. Supported extensions include .html, .htm, .xml, .svg, .rss, .atom, .gotmpl, and .txt. ```bash # Supported extensions: .html .htm .xml .svg .rss .atom .gotmpl .txt gotmplfmt -w mysite/ # Example directory: # mysite/index.html → formatted (HTML template) # mysite/partial.htm → formatted (HTML template) # mysite/feed.xml → formatted (XML template) # mysite/style.css → SKIPPED (not a template file) # Equivalent recursive check in CI: gotmplfmt -l mysite/ | grep . && exit 1 ``` -------------------------------- ### Format Template to Stdout (CLI) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Format a single template file and print the formatted output to standard output. The original file remains unchanged. ```bash # Input file: index.html #
# hello #
gotmplfmt index.html # Output (printed to stdout, uses tabs): #
# hello #
``` -------------------------------- ### CLI: Format to stdout Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Formats a single template file and prints the result to standard output without modifying the source file. ```APIDOC ## CLI: Format to stdout Formats a single template file and prints the result to standard output without modifying the source file. ### Usage ```bash gotmplfmt [file] ``` ### Example ```bash # Input file: index.html #
# hello #
gotmplfmt index.html # Output (printed to stdout, uses tabs): #
# hello #
``` ``` -------------------------------- ### Configure gotmplfmt VS Code extension Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Configure the gotmplfmt VS Code extension via `settings.json` to specify the binary path, language IDs, and set it as the default formatter for HTML files. Optionally enable format-on-save. ```jsonc // .vscode/settings.json { // Path to the gotmplfmt binary (defaults to "gotmplfmt" on PATH) "gotmplfmt.path": "/usr/local/bin/gotmplfmt", // Language IDs for which gotmplfmt provides formatting // Defaults: ["html", "gohtml", "gotmpl", "go-template"] "gotmplfmt.languages": ["html", "gohtml", "gotmpl", "go-template"], // Set gotmplfmt as the default formatter for HTML files "[html]": { "editor.defaultFormatter": "gohugoio.gotmplfmt" }, // Optionally enable format-on-save "editor.formatOnSave": true } ``` -------------------------------- ### Format Single File to Stdout Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/file.txt Use gotmplfmt to format a Go template file and output the result to standard output. This is useful for quick checks or piping to other commands. ```bash gotmplfmt input.html cmp stdout expected.html ``` -------------------------------- ### Show Diffs (CLI) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Display a unified diff of what would change if the file were formatted. Source files are not modified. No output is produced if the file is already formatted. ```bash gotmplfmt -d layouts/index.html # Output (unified diff format): diff layouts/index.html.orig layouts/index.html --- layouts/index.html.orig +++ layouts/index.html @@ -1,3 +1,3 @@
- hello + hello
# No output if file is already formatted: gotmplfmt -d layouts/partials/footer.html # (no output) ``` -------------------------------- ### CLI: Directory walking Source: https://context7.com/gohugoio/gotmplfmt/llms.txt When given a directory path, gotmplfmt recursively walks all subdirectories and processes every supported template file. Non-template files are skipped. ```APIDOC ## CLI: Directory walking When given a directory path, gotmplfmt recursively walks all subdirectories and processes every supported template file. Non-template files are skipped. ### Supported extensions .html .htm .xml .svg .rss .atom .gotmpl .txt ### Example ```bash # Supported extensions: .html .htm .xml .svg .rss .atom .gotmpl .txt gotmplfmt -w mysite/ # Example directory: # mysite/index.html → formatted (HTML template) # mysite/partial.htm → formatted (HTML template) # mysite/feed.xml → formatted (XML template) # mysite/style.css → SKIPPED (not a template file) # Equivalent recursive check in CI: gotmplfmt -l mysite/ | grep . && exit 1 ``` ``` -------------------------------- ### VSCE Login Source: https://github.com/gohugoio/gotmplfmt/blob/main/vscode/README.md Log in to the VSCode Marketplace using npx vsce. Requires a Personal Access Token (PAT) with Marketplace scope. ```bash npx vsce login gohugoio ``` -------------------------------- ### Verify Formatted Files Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/directory.txt Compare the formatted output of template files with their expected versions. Non-template files are also compared to ensure they remain unchanged. ```bash cmp mysite/index.html expected.html ``` ```bash cmp mysite/partial.htm expected.html ``` ```bash cmp mysite/feed.xml expected.xml ``` ```bash cmp mysite/style.css original.css ``` -------------------------------- ### List Unformatted Files (CLI) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt List only the files whose content differs from the formatted output, printing one path per line. Source files are not modified. This is useful for checking compliance without making changes. Already-formatted files produce no output. ```bash gotmplfmt -l layouts/ # Output (one path per line for each unformatted file): # layouts/partials/header.html # layouts/index.html # Already-formatted files produce no output: gotmplfmt -l layouts/partials/footer.html # (no output) ``` -------------------------------- ### Expected HTML Output Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/directory.txt The expected formatted version of the sample HTML content. ```html
hello
``` -------------------------------- ### Publish New Version Source: https://github.com/gohugoio/gotmplfmt/blob/main/vscode/README.md Compile the extension and publish a new version to the VSCode Marketplace using npx vsce. Specify the version bump (minor, patch, major) or a specific version number. ```bash cd vscode npm run compile npx vsce publish minor # or: patch, major, 0.2.0 ``` -------------------------------- ### Format Template In-Place (CLI) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Format a template file in place, overwriting the original source file with the formatted output. No output is written to stdout. This command can also process an entire directory tree recursively. ```bash gotmplfmt -w layouts/index.html # The file is rewritten with tab indentation. # Stdout remains empty. # Process an entire directory tree recursively: gotmplfmt -w layouts/ # Formats all .html, .htm, .xml, .svg, .rss, .atom, .gotmpl, .txt files # Non-template files (e.g. .css, .js) are silently skipped. ``` -------------------------------- ### Go Template License Source: https://github.com/gohugoio/gotmplfmt/blob/main/README.md The BSD-ish license for the Go standard library code that gotmplfmt is based on. ```text Copyright (c) 2009 The Go Authors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``` -------------------------------- ### Ignore specific regions with gotmplfmt-ignore-start/end Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Wrap a section of a template with `{{/* gotmplfmt-ignore-start */}}` and `{{/* gotmplfmt-ignore-end */}}` to preserve its formatting while allowing the rest of the file to be formatted normally. ```html {{ if .Foo }} {{/* gotmplfmt-ignore-start */}} {{ range .Bar }} {{ end }} {{/* gotmplfmt-ignore-end */}} {{ end }} {{ if .Foo }} {{/* gotmplfmt-ignore-start */}} {{ range .Bar }} {{ end }} {{/* gotmplfmt-ignore-end */}} {{ end }} ``` -------------------------------- ### CLI: Format from stdin Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Reads template content from standard input and writes formatted output to stdout. Useful for editor integrations and shell pipelines. ```APIDOC ## CLI: Format from stdin Reads template content from standard input and writes formatted output to stdout. Useful for editor integrations and shell pipelines. ### Usage ```bash echo "[template content]" | gotmplfmt ``` ### Example ```bash echo '
{{ .Title }}
' | gotmplfmt # Output: #
# {{ .Title }} #
# Error: -w and -l flags cannot be used with stdin gotmplfmt -w # => error: cannot use -w with standard input gotmplfmt -l # => error: cannot use -l with standard input ``` ``` -------------------------------- ### Expected XML Output Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/directory.txt The expected formatted version of the sample XML content. ```xml ``` -------------------------------- ### Format Template Files in Directory Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/directory.txt Use `gotmplfmt -w` to format template files within a directory. This command modifies files in place. ```bash gotmplfmt -w mysite ``` -------------------------------- ### Build and Process CSS with Hugo Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/out/cssbuild.html Use this snippet to process a CSS file using Hugo's `css.Build` function. Configure options like loaders, minification, source maps, and target browsers. ```html {{ with resources.Get "css/main.css" }} {{ $opts := dict "loaders" (dict ".png" "dataurl" ".svg" "dataurl") "minify" (cond hugo.IsDevelopment false true) "sourceMap" (cond hugo.IsDevelopment "linked" "none") "target" (slice "chrome115" "edge115" "firefox116" "ios16.4" "opera101" "safari16.4") }} {{ with . | css.Build $opts }} {{ if hugo.IsDevelopment }} {{ else }} {{ with . | fingerprint }} {{ end }} {{ end }} {{ end }} {{ end }} ``` -------------------------------- ### CLI: Show diffs (-d) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Displays a unified diff of what would change if the file were formatted. Source files are not modified. ```APIDOC ## CLI: Show diffs (-d) Displays a unified diff of what would change if the file were formatted. Source files are not modified. ### Usage ```bash gotmplfmt -d [file] ``` ### Example ```bash gotmplfmt -d layouts/index.html # Output (unified diff format): # diff layouts/index.html.orig layouts/index.html # --- # layouts/index.html.orig # +++ layouts/index.html # @@ -1,3 +1,3 @@ #
# - hello # +\thello #
# No output if file is already formatted: gotmplfmt -d layouts/partials/footer.html # (no output) ``` ``` -------------------------------- ### Format HTML from Stdin with Gotmplfmt Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/stdin.txt Use gotmplfmt to format HTML content piped from stdin. Compare the output to an expected file. ```bash stdin input.html gotmplfmt cmp stdout expected.html ``` -------------------------------- ### Go API: format.Format Source: https://context7.com/gohugoio/gotmplfmt/llms.txt The core formatting function, exported from the `internal/format` package. Accepts a raw template string, normalizes CRLF line endings to LF, parses the template AST, and returns the formatted string. If the file begins with `{{/* gotmplfmt-ignore-all */}}`, the original text is returned unchanged. ```APIDOC ## Go API: `format.Format` The core formatting function, exported from the `internal/format` package. Accepts a raw template string, normalizes CRLF line endings to LF, parses the template AST, and returns the formatted string. If the file begins with `{{/* gotmplfmt-ignore-all */}}`, the original text is returned unchanged. ### Signature ```go func Format(src string) (string, error) ``` ### Example ```go import "github.com/gohugoio/gotmplfmt/internal/format" src := `
{{ if .Active }}active{{ end }}
` out, err := format.Format(src) if err != nil { log.Fatalf("format error: %v", err) } fmt.Print(out) // Output: //
// {{ if .Active }}active{{ end }} //
// CRLF line endings are normalized automatically: crlfSrc := "
\r\n hi\r\n
" out2, _ := format.Format(crlfSrc) fmt.Print(out2) // Output (LF only): //
// hi //
``` ``` -------------------------------- ### Check Formatting in CI Source: https://github.com/gohugoio/gotmplfmt/blob/main/README.md Use the -l flag with gotmplfmt to list files that differ in formatting. This is useful for CI checks to ensure code consistency. ```bash gotmplfmt -l . | grep . && exit 1 ``` -------------------------------- ### Format Template from Stdin (CLI) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Read template content from standard input and write the formatted output to standard output. This is useful for editor integrations and shell pipelines. The -w and -l flags cannot be used with stdin. ```bash echo '
{{ .Title }}
' | gotmplfmt # Output: #
# {{ .Title }} #
# Error: -w and -l flags cannot be used with stdin gotmplfmt -w # => error: cannot use -w with standard input gotmplfmt -l # => error: cannot use -l with standard input ``` -------------------------------- ### Display Diffs in CI Source: https://github.com/gohugoio/gotmplfmt/blob/main/README.md Use the -d flag with gotmplfmt to display differences in formatting. This can be helpful for debugging formatting issues in CI. ```bash gotmplfmt -d . ``` -------------------------------- ### Configure HTML as Default Formatter Source: https://github.com/gohugoio/gotmplfmt/blob/main/vscode/README.md Configure VSCode to use gotmplfmt as the default formatter for HTML files by adding this to your settings.json. ```json [html]: { "editor.defaultFormatter": "gohugoio.gotmplfmt" } ``` -------------------------------- ### Verify Source File Unchanged Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/diff-flag.txt After running gotmplfmt with the -d flag, verify that the original source file has not been modified. ```bash cmp unformatted.html unformatted.orig.html ``` -------------------------------- ### List files with differing formatting using -l Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/list-flag.txt Use the -l flag to list files that have formatting differences. This command will output 'unformatted.html' because its formatting differs from the standard, but not 'formatted.html'. ```bash gotmplfmt -l unformatted.html formatted.html stdout '^unformatted.html$' ! stdout '^formatted.html$' ``` -------------------------------- ### Verify Original File Unchanged Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/file.txt After formatting, compare the input file with an original version to ensure that the formatting process did not modify the source file. ```bash cmp input.html original.html ``` -------------------------------- ### Check template formatting in GitHub Actions Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Integrate gotmplfmt into GitHub Actions to enforce template formatting. Use the `-d` flag to fail the build if any file is unformatted. ```yaml # .github/workflows/format-check.yml name: Check Go Template Formatting on: [push, pull_request] jobs: format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version: "stable" - name: Install gotmplfmt run: go install github.com/gohugoio/gotmplfmt@latest - name: Check template formatting run: "diff <(gotmplfmt -d layouts) <(printf '')" # Fails if any file in layouts/ differs from formatted output # Alternative: list unformatted files and fail if any are found - name: List unformatted templates run: gotmplfmt -l layouts | grep . && exit 1 || exit 0 ``` -------------------------------- ### Ignore all formatting with gotmplfmt-ignore-all Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Use `{{/* gotmplfmt-ignore-all */}}` to prevent any formatting changes in a template file. This is useful for files with intentionally non-standard formatting. ```html {{/* gotmplfmt-ignore-all */}} {{ if .Foo }} {{ end }} ``` -------------------------------- ### CLI: List unformatted files (-l) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Lists only the files whose content differs from the formatted output. Source files are not modified. Useful for checking compliance without making changes. ```APIDOC ## CLI: List unformatted files (-l) Lists only the files whose content differs from the formatted output. Source files are not modified. Useful for checking compliance without making changes. ### Usage ```bash gotmplfmt -l [file_or_directory] ``` ### Example ```bash gotmplfmt -l layouts/ # Output (one path per line for each unformatted file): # layouts/partials/header.html # layouts/index.html # Already-formatted files produce no output: gotmplfmt -l layouts/partials/footer.html # (no output) ``` ``` -------------------------------- ### CLI: Write result back to file (-w) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Formats a file in place, overwriting the source with formatted output. No output is written to stdout. Can process entire directory trees recursively. ```APIDOC ## CLI: Write result back to file (-w) Formats a file in place, overwriting the source with formatted output. No output is written to stdout. ### Usage ```bash gotmplfmt -w [file_or_directory] ``` ### Example ```bash gotmplfmt -w layouts/index.html # The file is rewritten with tab indentation. # Stdout remains empty. # Process an entire directory tree recursively: gotmplfmt -w layouts/ # Formats all .html, .htm, .xml, .svg, .rss, .atom, .gotmpl, .txt files # Non-template files (e.g. .css, .js) are silently skipped. ``` ``` -------------------------------- ### Format Go Template String (API) Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Use the `format.Format` function from the `internal/format` package to format a raw template string. It normalizes CRLF line endings to LF and parses the template AST. If the file begins with `{{/* gotmplfmt-ignore-all */}}`, the original text is returned unchanged. ```go import "github.com/gohugoio/gotmplfmt/internal/format" src := `
{{ if .Active }}active{{ end }}
` out, err := format.Format(src) if err != nil { log.Fatalf("format error: %v", err) } fmt.Print(out) // Output: //
// {{ if .Active }}active{{ end }} //
// CRLF line endings are normalized automatically: crlfSrc := "
\r\n hi\r\n
" out2, _ := format.Format(crlfSrc) fmt.Print(out2) // Output (LF only): //
// hi //
``` -------------------------------- ### Generating Links with Variable Substitution in HTML Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/out/with-if-oneliners.html Construct HTML links where attributes like the text and URL can be dynamically set using template variables. ```html [{{ .Text }}](foo "{{ . }}") ``` -------------------------------- ### Conditional Rendering in HTML Templates Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/out/with-if-oneliners.html Use 'if' statements for conditional rendering of content. The 'else' clause provides alternative content when the condition is false. ```html {{ if true }}foo{{ end }} ``` ```html {{ if true }}foo{{ else }}bar{{ end }} ``` -------------------------------- ### gotmplfmt CLI Usage Flags Source: https://github.com/gohugoio/gotmplfmt/blob/main/README.md Common flags for the gotmplfmt CLI tool. Use these to control output and behavior. ```bash usage: gotmplfmt [flags] [path ...] -d display diffs instead of rewriting files -l list files whose formatting differs from gotmplfmt's -w write result to (source) file instead of stdout -version print version information and exit ``` -------------------------------- ### Ignore Multiline Go Template Formatting Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/in/ignore-multiline.html Use gotmplfmt-ignore-start and gotmplfmt-ignore-end comments to prevent gotmplfmt from reformatting the Go template code between these markers. This is useful for preserving specific multiline structures or comments. ```gohtml {{ if .Foo }} {{/\* gotmplfmt-ignore-start \*/}} {{ range .Bar }} {{ end }} {{/\* gotmplfmt-ignore-end \*/}} {{ end }} ``` -------------------------------- ### Error Handling: -w Flag with Stdin in Gotmplfmt Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/stdin.txt Test that using the -w flag with standard input to gotmplfmt results in an error message. ```bash ! gotmplfmt -w stderr 'error: cannot use -w with standard input' ``` -------------------------------- ### Format if/else if chains in templates Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Multi-branch conditional statements are formatted with consistent indentation at each level, improving readability. ```html {{ if true }} 1 {{ else if false }} 2 {{ else if false }} 3 {{ end }} {{ if true }} 1 {{ else if false }} 2 {{ else if false }} 3 {{ end }} ``` -------------------------------- ### Preserve Go template trim markers Source: https://context7.com/gohugoio/gotmplfmt/llms.txt Go template trim markers (`{{-` and `-}}`) are respected and preserved during the formatting process. ```plaintext {{- range .Items -}} A {{ .Name }} {{- end -}} ``` -------------------------------- ### Display Diffs with -d Flag Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/diff-flag.txt Use the -d flag to show differences between the original and formatted versions of a template file. This is useful for reviewing changes before applying them. ```bash gotmplfmt -d unformatted.html stdout 'diff' stdout '- hello' ``` ```bash gotmplfmt -d formatted.html ! stdout . ``` -------------------------------- ### Hugo With Statement Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/in/with-if-oneliners.html The 'with' statement is useful for checking if a variable is non-empty and setting a context. If the variable is empty, the 'else' block is executed. ```html {{ with true}}{{ . }}{{ else }}bar{{ end }} ``` -------------------------------- ### Ignore Multiline Go Template Formatting Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/out/ignore-multiline.html Use gotmplfmt-ignore-start and gotmplfmt-ignore-end comments to prevent formatting within a multiline Go template block. This is useful for preserving specific whitespace or structure that the formatter might otherwise alter. ```gohtml {{ if .Foo }} {{/* gotmplfmt-ignore-start */}} {{ range .Bar }} {{ end }} {{/* gotmplfmt-ignore-end */}} {{ end }} ``` -------------------------------- ### Handle -l flag with standard input Source: https://github.com/gohugoio/gotmplfmt/blob/main/testscripts/list-flag.txt The -l flag cannot be used with standard input. Attempting to do so will result in an error message indicating this limitation. ```bash ! gotmplfmt -l stderr 'error: cannot use -l with standard input' ``` -------------------------------- ### Hugo If/Else Statement Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/in/with-if-oneliners.html Use the 'if' statement for simple conditional rendering. The 'else' block executes when the condition is false. ```html {{ if true}}foo{{ end }} ``` ```html {{ if true}}foo{{ else }}bar{{ end }} ``` -------------------------------- ### Format conditional attributes in HTML Source: https://context7.com/gohugoio/gotmplfmt/llms.txt gotmplfmt correctly aligns Go template actions within HTML attributes, ensuring proper indentation for `{{- with }}`, `{{- else }}`, and `{{- end }}` relative to their containing element. ```html
{{ end }} {{ if true }}
{{ end }} ``` -------------------------------- ### Variable Scoping with 'with' in HTML Templates Source: https://github.com/gohugoio/gotmplfmt/blob/main/internal/format/golden/out/with-if-oneliners.html The 'with' statement sets a new block scope. If the given variable is truthy, the block is executed; otherwise, the 'else' block is executed. ```html {{ with true }}{{ . }}{{ else }}bar{{ end }} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.