Home > Backend Development > Golang > How to Access Parent/Global Pipeline Values within Range in Go Templates?

How to Access Parent/Global Pipeline Values within Range in Go Templates?

Barbara Streisand
Release: 2024-11-21 01:03:11
Original
400 people have browsed it

How to Access Parent/Global Pipeline Values within Range in Go Templates?

Accessing Parent/Global Pipeline Within Range in Go Templates

To access the parent or global pipeline value within a range action in the text/template package, there are two main methods:

Using the $ Variable (Recommended)

According to the text/template documentation, when execution begins, the $ variable is set to the data argument passed to Execute, effectively representing the starting value of the dot. This allows access to the outer scope variables, including the parent/global pipeline. For instance, to access the Path in the outer scope, use $.Path.

const page = `{{range .Files}}<script src="{{html $.Path}}/js/{{html .}}"></script>{{end}}`
Copy after login

Using a Custom Variable (Legacy Approach)

Alternatively, a custom variable can be created to pass values into the range scope:

const page = `{{$p := .Path}}{{range .Files}}<script src="{{html $p}}/js/{{html .}}"></script>{{end}}`
Copy after login

The above is the detailed content of How to Access Parent/Global Pipeline Values within Range in Go Templates?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template