HUGO

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于 Hugo
    • In this section
    • Introduction
    • Features
    • Privacy
    • Security
    • License
  • 安装
    • In this section
    • macOS
    • Linux
    • Windows
    • BSD
  • 入门
    • In this section
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • In this section
    • Emojis
    • Functions
    • Methods
    • Page collections
  • 内容管理
    • In this section
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
    • Data sources
    • Content adapters
  • 模板
    • In this section
    • Introduction
    • Template types
    • Lookup order
    • Base templates
    • Home templates
    • Single templates
    • Section templates
    • Taxonomy templates
    • Term templates
    • Partial templates
    • Content view templates
    • Shortcode templates
    • Sitemap templates
    • RSS templates
    • 404 templates
    • robots.txt templates
    • Menus
    • Pagination
    • Embedded templates
    • Custom output formats
  • 函数
    • In this section
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • 方法
    • In this section
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pager
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • In this section
    • Introduction
    • Blockquotes
    • Code blocks
    • Headings
    • Images
    • Links
    • Passthrough
    • Tables
  • Hugo Modules
    • In this section
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • In this section
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • 故障排除
    • In this section
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • 开发工具
    • In this section
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • 托管 & 部署
    • In this section
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • 贡献
    • In this section
    • Development
    • Documentation
    • Themes
  • Maintenance
CONTENT MANAGEMENT

Data sources

Use local and remote data sources to augment or create content.

Hugo can access and unmarshal local and remote data sources including CSV, JSON, TOML, YAML, and XML. Use this data to augment existing content or to create new content.

A data source might be a file in the data directory, a global resource, a page resource, or a remote resource.

Data directory

The data directory in the root of your project may contain one or more data files, in either a flat or nested tree. Hugo merges the data files to create a single data structure, accessible with the Data method on a Site object.

Hugo also merges data directories from themes and modules into this single data structure, where the data directory in the root of your project takes precedence.

Hugo reads the combined data structure into memory and keeps it there for the entire build. For data that is infrequently accessed, use global or page resources instead.

Theme and module authors may wish to namespace their data files to prevent collisions. For example:

project/
└── data/
    └── mytheme/
        └── foo.json

Do not place CSV files in the data directory. Access CSV files as page, global, or remote resources.

See the documentation for the Data method on a Site object for details and examples.

Global resources

Use the resources.Get and transform.Unmarshal functions to access data files that exist as global resources.

See the transform.Unmarshal documentation for details and examples.

Page resources

Use the Resources.Get method on a Page object combined with the transform.Unmarshal function to access data files that exist as page resources.

See the transform.Unmarshal documentation for details and examples.

Remote resources

Use the resources.GetRemote and transform.Unmarshal functions to access remote data.

See the transform.Unmarshal documentation for details and examples.

Augment existing content

Use data sources to augment existing content. For example, create a shortcode to render an HTML table from a global CSV resource.

assets/pets.csv
"name","type","breed","age"
"Spot","dog","Collie","3"
"Felix","cat","Malicious","7"
content/example.md
{{< csv-to-table "pets.csv" >}}
layouts/shortcodes/csv-to-table.html
{{ with $file := .Get 0 }}
  {{ with resources.Get $file }}
    {{ with . | transform.Unmarshal }}
      <table>
        <thead>
          <tr>
            {{ range index . 0 }}
              <th>{{ . }}</th>
            {{ end }}
          </tr>
        </thead>
        <tbody>
          {{ range after 1 . }}
            <tr>
              {{ range . }}
                <td>{{ . }}</td>
              {{ end }}
            </tr>
          {{ end }}
        </tbody>
      </table>
    {{ end }}
  {{ else }}
    {{ errorf "The %q shortcode was unable to find %s. See %s" $.Name $file $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requires one positional argument, the path to the CSV file relative to the assets directory. See %s" .Name .Position }}
{{ end }}

Hugo renders this to:

name type breed age
Spot dog Collie 3
Felix cat Malicious 7

Create new content

Use content adapters to create new content.

See also

  • Configure Hugo
  • Front matter

On this page

  • Data directory
  • Global resources
  • Page resources
  • Remote resources
  • Augment existing content
  • Create new content
Last updated: June 24, 2024: Fix typo (5d7317c8)
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays
 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于 Hugo
  • 安装
  • 入门
  • Quick reference
  • 内容管理
  • 模板
  • 函数
  • 方法
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • 故障排除
  • 开发工具
  • 托管 & 部署
  • 贡献
  • Maintenance