transform.Unmarshal
Syntax
transform.Unmarshal [选项] 输入
Returns
any
Alias
unmarshal
输入可以是字符串或资源。
解析字符串
{{ $string := `
title: Les Misérables
author: Victor Hugo
`}}
{{ $book := unmarshal $string }}
{{ $book.title }} → Les Misérables
{{ $book.author }} → Victor Hugo
解析资源
可以使用transform.Unmarshal
函数处理全局资源,页面资源和远程资源。
全局资源
全局资源是存在于assets目录或挂载到assets目录的任何目录中的文件。
assets/
└── data/
└── books.json
{{ $data := "" }}
{{ $path := "data/books.json" }}
{{ with resources.Get $path }}
{{ with unmarshal . }}
{{ $data = . }}
{{ end }}
{{ else }}
{{ errorf "无法获取全局资源 %q" $path }}
{{ end }}
{{ range where $data "author" "Victor Hugo" }}
{{ .title }} → Les Misérables
{{ end }}
页面资源
页面资源是存在于页面束中的文件。
content/
├── post/
│ └── book-reviews/
│ ├── books.json
│ └── index.md
└── _index.md
{{ $data := "" }}
{{ $path := "books.json" }}
{{ with .Resources.Get $path }}
{{ with unmarshal . }}
{{ $data = . }}
{{ end }}
{{ else }}
{{ errorf "无法获取页面资源 %q" $path }}
{{ end }}
{{ range where $data "author" "Victor Hugo" }}
{{ .title }} → Les Misérables
{{ end }}
远程资源
远程资源是通过HTTP或HTTPS访问的远程服务器上的文件。
{{ $data := "" }}
{{ $url := "https://example.org/books.json" }}
{{ with resources.GetRemote $url }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "无法获取远程资源 %q" $url }}
{{ end }}
{{ range where $data "author" "Victor Hugo" }}
{{ .title }} → Les Misérables
{{ end }}
选项
在解析CSV文件时,可以提供一个可选选项映射。
- delimiter
- (
string
) 使用的分隔符,默认为,
。 - comment
- (
string
) 在CSV中使用的注释字符。如果设置,不带前置空格的以注释字符开头的行将被忽略。
{{ $csv := "a;b;c" | transform.Unmarshal (dict "delimiter" ";") }}
处理XML
当解析XML文件时,请在访问数据时不包括根节点。例如,在解析以下RSS订阅后,使用$data.channel.title
访问订阅的标题。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Books on Example Site</title>
<link>https://example.org/books/</link>
<description>Recent content in Books on Example Site</description>
<language>en-US</language>
<atom:link href="https://example.org/books/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>The Hunchback of Notre Dame</title>
<description>Written by Victor Hugo</description>
<link>https://example.org/books/the-hunchback-of-notre-dame/</link>
<pubDate>Mon, 09 Oct 2023 09:27:12 -0700</pubDate>
<guid>https://example.org/books/the-hunchback-of-notre-dame/</guid>
</item>
<item>
<title>Les Misérables</title>
<description>Written by Victor Hugo</description>
<link>https://example.org/books/les-miserables/</link>
<pubDate>Mon, 09 Oct 2023 09:27:11 -0700</pubDate>
<guid>https://example.org/books/les-miserables/</guid>
</item>
</channel>
</rss>
获取远程数据:
{{ $data := "" }}
{{ $url := "https://example.org/books/index.xml" }}
{{ with resources.GetRemote $url }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "无法获取远程资源 %q" $url }}
{{ end }}
检查数据结构:
<pre>{{ jsonify (dict "indent" " ") $data }}</pre>
列出书籍标题:
{{ with $data.channel.item }}
<ul>
{{ range . }}
<li>{{ .title }}</li>
{{ end }}
</ul>
{{ end }}
Hugo通过渲染生成以下结果:
<ul>
<li>The Hunchback of Notre Dame</li>
<li>Les Misérables</li>
</ul>
XML属性和命名空间
让我们为RSS订阅的title
节点添加一个lang
属性,并添加一个ISBN号码的命名空间节点:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:isbn="http://schemas.isbn.org/ns/1999/basic.dtd"
>
<channel>
<title>Books on Example Site</title>
<link>https://example.org/books/</link>
<description>Recent content in Books on Example Site</description>
<language>en-US</language>
<atom:link href="https://example.org/books/index.xml" rel="self" type="application/rss+xml" />
<item>
<title lang="fr">The Hunchback of Notre Dame</title>
<description>Written by Victor Hugo</description>
<isbn:number>9780140443530</isbn:number>
<link>https://example.org/books/the-hunchback-of-notre-dame/</link>
<pubDate>Mon, 09 Oct 2023 09:27:12 -0700</pubDate>
<guid>https://example.org/books/the-hunchback-of-notre-dame/</guid>
</item>
<item>
<title lang="en">Les Misérables</title>
<description>Written by Victor Hugo</description>
<isbn:number>9780451419439</isbn:number>
<link>https://example.org/books/les-miserables/</link>
<pubDate>Mon, 09 Oct 2023 09:27:11 -0700</pubDate>
<guid>https://example.org/books/les-miserables/</guid>
</item>
</channel>
</rss>
在获取远程数据后,检查数据结构:
<pre>{{ jsonify (dict "indent" " ") $data }}</pre>
每个item
节点如下所示:
{
"description": "Written by Victor Hugo",
"guid": "https://example.org/books/the-hunchback-of-notre-dame/",
"link": "https://example.org/books/the-hunchback-of-notre-dame/",
"number": "9780140443530",
"pubDate": "Mon, 09 Oct 2023 09:27:12 -0700",
"title": {
"#text": "The Hunchback of Notre Dame",
"-lang": "fr"
}
}
标题键不以下划线或字母开头—它们不是有效的标识符。可以使用index
函数来访问值:
{{ with $data.channel.item }}
<ul>
{{ range . }}
{{ $title := index .title "#text" }}
{{ $lang := index .title "-lang" }}
{{ $ISBN := .number }}
<li>{{ $title }} ({{ $lang }}) {{ $ISBN }}</li>
{{ end }}
</ul>
{{ end }}
Hugo通过渲染生成以下结果:
<ul>
<li>The Hunchback of Notre Dame (fr) 9780140443530</li>
<li>Les Misérables (en) 9780451419439</li>
</ul>