From 0eaaa8fee37068bfc8ecfb760f770ecc9a7af22a Mon Sep 17 00:00:00 2001 From: Paul van Brouwershaven Date: Thu, 2 Dec 2021 17:30:36 +0100 Subject: Implement XML data support Example: ``` {{ with resources.Get "https://example.com/rss.xml" | transform.Unmarshal }} {{ range .channel.item }} {{ .title | plainify | htmlUnescape }}

{{ .description | plainify | htmlUnescape }}

{{ $link := .link | plainify | htmlUnescape }} {{ $link }}

{{ end }} {{ end }} ``` Closes #4470 --- docs/content/en/functions/transform.Unmarshal.md | 31 +++++++++++++++++++++++- docs/content/en/templates/data-templates.md | 10 +++++--- 2 files changed, 36 insertions(+), 5 deletions(-) (limited to 'docs') diff --git a/docs/content/en/functions/transform.Unmarshal.md b/docs/content/en/functions/transform.Unmarshal.md index 973779c4c..9b380dc57 100644 --- a/docs/content/en/functions/transform.Unmarshal.md +++ b/docs/content/en/functions/transform.Unmarshal.md @@ -1,6 +1,6 @@ --- title: "transform.Unmarshal" -description: "`transform.Unmarshal` (alias `unmarshal`) parses the input and converts it into a map or an array. Supported formats are JSON, TOML, YAML and CSV." +description: "`transform.Unmarshal` (alias `unmarshal`) parses the input and converts it into a map or an array. Supported formats are JSON, TOML, YAML, XML and CSV." date: 2018-12-23 categories: [functions] menu: @@ -45,3 +45,32 @@ Example: ```go-html-template {{ $csv := "a;b;c" | transform.Unmarshal (dict "delimiter" ";") }} ``` + +## XML data + +As a convenience, Hugo allows you to access XML data in the same way that you access JSON, TOML, and YAML: you do not need to specify the root node when accessing the data. + +To get the contents of `` in the document below, you use `{{ .message.title }}`: + +``` +<root> + <message> + <title>Hugo rocks! + Thanks for using Hugo + + +``` + +The following example lists the items of an RSS feed: + +``` +{{ with resources.Get "https://example.com/rss.xml" | transform.Unmarshal }} + {{ range .channel.item }} + {{ .title | plainify | htmlUnescape }}
+

{{ .description | plainify | htmlUnescape }}

+ {{ $link := .link | plainify | htmlUnescape }} + {{ $link }}
+
+ {{ end }} +{{ end }} +``` diff --git a/docs/content/en/templates/data-templates.md b/docs/content/en/templates/data-templates.md index c36344776..441cc2f10 100644 --- a/docs/content/en/templates/data-templates.md +++ b/docs/content/en/templates/data-templates.md @@ -6,7 +6,7 @@ date: 2017-02-01 publishdate: 2017-02-01 lastmod: 2017-03-12 categories: [templates] -keywords: [data,dynamic,csv,json,toml,yaml] +keywords: [data,dynamic,csv,json,toml,yaml,xml] menu: docs: parent: "templates" @@ -20,7 +20,7 @@ toc: true -Hugo supports loading data from YAML, JSON, and TOML files located in the `data` directory in the root of your Hugo project. +Hugo supports loading data from YAML, JSON, XML, and TOML files located in the `data` directory in the root of your Hugo project. {{< youtube FyPgSuwIMWQ >}} @@ -28,7 +28,7 @@ Hugo supports loading data from YAML, JSON, and TOML files located in the `data` The `data` folder is where you can store additional data for Hugo to use when generating your site. Data files aren't used to generate standalone pages; rather, they're meant to be supplemental to content files. This feature can extend the content in case your front matter fields grow out of control. Or perhaps you want to show a larger dataset in a template (see example below). In both cases, it's a good idea to outsource the data in their own files. -These files must be YAML, JSON, or TOML files (using the `.yml`, `.yaml`, `.json`, or `.toml` extension). The data will be accessible as a `map` in the `.Site.Data` variable. +These files must be YAML, JSON, XML, or TOML files (using the `.yml`, `.yaml`, `.json`, `.xml`, or `.toml` extension). The data will be accessible as a `map` in the `.Site.Data` variable. ## Data Files in Themes @@ -95,7 +95,7 @@ Discover a new favorite bass player? Just add another `.toml` file in the same d ## Example: Accessing Named Values in a Data File -Assume you have the following data structure in your `User0123.[yml|toml|json]` data file located directly in `data/`: +Assume you have the following data structure in your `User0123.[yml|toml|xml|json]` data file located directly in `data/`: {{< code-toggle file="User0123" >}} Name: User0123 @@ -232,6 +232,7 @@ If you change any local file and the LiveReload is triggered, Hugo will read the * [YAML Spec][yaml] * [JSON Spec][json] * [CSV Spec][csv] +* [XML Spec][xml] [config]: /getting-started/configuration/ [csv]: https://tools.ietf.org/html/rfc4180 @@ -247,3 +248,4 @@ If you change any local file and the LiveReload is triggered, Hugo will read the [variadic]: https://en.wikipedia.org/wiki/Variadic_function [vars]: /variables/ [yaml]: https://yaml.org/spec/ +[xml]: https://www.w3.org/XML/ -- cgit v1.2.3